5

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

Dineshkani
  • 2,899
  • 7
  • 31
  • 43

3 Answers3

6

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

Benjamin P.
  • 453
  • 5
  • 12
4

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:)

dreamweiver
  • 6,002
  • 2
  • 24
  • 39
0

I discovered that textarea is its own input type, as it were, so you would need to make your first line simply include that, and you will have live updates as you type...

$("#inptxt").on("change input paste keyup textarea", function() {
   $("#res").html(jQuery(this).val());
});
$("#but").bind("click",function(){
    $("#inptxt").val("Jquery not trigger the change function");
});

http://jsfiddle.net/7wnrxh6c/

NovaDev
  • 2,737
  • 5
  • 29
  • 43