Jquery check if input value has changed dynamically

I need to accomplish the following problem. I have an input and I need to show a warning message if its value has changed. But for some reason the warning shows multiple times, can't figure out why. Here's the first question.

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

      

Second question:

How can I check if the input value has changed dynamically. For example, if I click on some element, then the value changes.

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

      

Here's the fiddle I created.

Any ideas? Thanks in advance!

+3


source to share


3 answers


When the value of the inputs changes, no event occurs dynamically. Your best option is to manually raise the event when the value changes:



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

      

+4


source


There is no event for this, you should instead use trigger the change event

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

      



DEMO

0


source


When the value change value dynamically, you have to bind the event to the element, see below sample

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

      

0


source







All Articles