Elad Katz
Elad Katz

Reputation: 7591

catching value changes in input, when it's not focused

i want to create an attribute (e.g. markChanges='true') that will automatically detects if the value of the input has changed, but only if that input is not focused.

(the value could change from js code of course)

How would i do that?

Upvotes: 1

Views: 503

Answers (1)

vinceh
vinceh

Reputation: 3510

Seems like the only solution would be to poll, which means you would have to use setInterval(). Check out this fiddle for an example. Code is

function detectChange(input, interval) {
    var previous = "";

    setInterval( function() {

        var val = $(input).val();
        if (  val != previous ) {
            previous = val;
            alert("Input has changed");
        }

    }, interval);
}

detectChange("input", 100);

setTimeout( function() {
    $("input").val("some value!");
}, 4000);

Upvotes: 2

Related Questions