lalexa
lalexa

Reputation: 453

jquery check if input value has changed dynamically

I have to implement following issue. I have input and i need to show warning message if its value has changed. But for some reasons alert shows few times, can't figure out why. Here was the first question.

$("#input").bind("propertychange change paste input", function(){
    alert("Value has been changed");
});

The second question:

How can i check if input's value has changed dynamically. For example if i click on some element then value changes.

$("a").click(function(){
    $("#input").val("test");
})

Here's a fiddle i've created.

Any ideas? Thanks in advance!

Upvotes: 5

Views: 10549

Answers (3)

Girish
Girish

Reputation: 12127

When value change value dynamically then you should bind event with element, see below sample

$("a").click(function(){
    $("#input").val("test").change();
})

Upvotes: 0

Balachandran
Balachandran

Reputation: 9637

There is no event for that instead you have to use trigger the change event

 $("a").click(function(){
    $("#input").val("test");
    $("#input").trigger("change");
})

DEMO

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337713

There is no event raised when an inputs value is changed dynamically. Your best option is to manually raise an event upon changing the value:

$("a").click(function(){
    $("#input").val("test").trigger('change');
})

Upvotes: 7

Related Questions