I try to trigger the event while changing the value in textarea. I bind the change input paste keyup
and its works fine when I type the content manually or paste. But I dont know how to trigger when changes occur using jquery. $("#inptxt").val("Jquery");
HTML
<textarea id="inptxt" ></textarea>
<div id="res" ></div>
<input type="button" value="click" id="but" />
jQuery
$("#inptxt").on("change input paste keyup", function() {
$("#res").html(jQuery(this).val());
});
$("#but").bind("click",function(){
$("#inptxt").val("Jquery");
});
Example Fiddle
$('. detect-change') . on('change cut paste', function(e) { console.
jQuery change() Method The change event occurs when the value of an element has been changed (only works on <input>, <textarea> and <select> elements). The change() method triggers the change event, or attaches a function to run when a change event occurs.
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.
val() doesn't trigger change() in jQuery.
Set a value per script does not trigger a change event. If you need the event you must manually trigger it.
$("#but").bind("click",function(){
$("#inptxt").val("Jquery").trigger('change');
});
jsfiddle
Browser doesn't recognize the text updation within the input field #inptxt
, as it is done programmatically using jquery. so you have to manually trigger change event after updating the text value.
Jquery Code:
$("#inptxt").on("change input paste keyup", function() {
$("#res").html(jQuery(this).val());
});
$("#but").bind("click",function(){
$("#inptxt").val("Jquery").change();
});
Live Demo:
http://jsfiddle.net/dreamweiver/ZVcG4/6/
Reference SO Question :Why does the jquery change event not trigger when I set the value of a select using val()?
Happy Coding:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With