user3700786
user3700786

Reputation: 149

Detection of textarea value change

How can I detect, that value in my textareaId has changed when the change was caused by javascript?

eg.

$("#buttonID").on("click, function(){
        $("#textareaID").val("lorem ipsum");
});

$("#textareaID").change(function(){
      //not working 
}); 

$("#textareaID").bind('input propertychange', function(){
  //not working 
});

Upvotes: 2

Views: 220

Answers (2)

GuyH
GuyH

Reputation: 796

Changing the value from your script doesn't actually fire any of the change or related events, you have to do that yourself at the same time using jQuery's trigger() method:-

$("#buttonID").click( function(){
    $("#textareaID").val("Value now added");
    $('#textareaID').trigger('change');   // add this line
});


$("#textareaID").change(function(){
    alert("here");
}); 

Same goes for the input/property change events. You would have to add:

    $('#textareaID').trigger('input propertychange');

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337713

When you change the value of an input programmatically no event is raised by default. If you need this behaviour you need to fire the event yourself, eg:

$('#foo').val('bar').trigger('change');

Upvotes: 2

Related Questions