I am trying to show alert if input value change. I am using jQuery, so here is a demo what i am trying to do but alert not showing / not working. Thanks
$(".d1, .d2").click( function (e) { var $this = $(this); $(".input").attr('value', $this.text()); }); $(".input").change( function() { alert(this.value); });
.d1, .d2 {cursor:pointer;border:1px solid black;margin-top:10px;padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <input class="input" value="" /><p/> <a class="d1">Demo 1</a> <a class="d2">Demo 2</a>
You need to use .val()
method to set value of input
element, Since you are using the change
event in input
on programatically changing the value it will not execute. You need to use .trigger(event)
Execute all handlers and behaviors attached to the matched elements for the given event type.
$(".d1, .d2").click( function (e) { var $this = $(this); $(".input").val($this.text()).trigger('change'); }); $(".input").change( function() { alert(this.value); });
.d1, .d2 {cursor:pointer;border:1px solid black;margin-top:10px;padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <input class="input" value="" /><p/> <a class="d1">Demo 1</a> <a class="d2">Demo 2</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.