Morta 1 Morta 1 - 1 year ago 74
Javascript Question

Catching a value change inside an input

I have an input that suppose to contain a name, the input has change and keyup events bounded to itself, at some point in my code i'm setting the value of the input myself, but the change/keyup event doesn't catch the changed value inside the input.

here's a snippet:

<input class="form-control inp-playlist-name" type="text" name="playlist" value="" placeholder="e.g. Have a Nice Day">

$('.inp-playlist-name').bind('change keyup', function(){
//do something

//at some point i change the value
$('.inp-playlist-name').val('A value');

is there any other event that can catch that kind of change?

Answer Source

There's no change event if you use Javascript to change the field's value. Instead, you can trigger the event yourself after making the change.

$('.inp-playlist-name').val('A value').trigger('change');

(Keep in mind that, if you're not careful, it's possible to end up with an infinite loop when doing this.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download