0

According to the official documentation, this event is:

Triggered when the field is blurred, if the value has changed;

Now, how does it know if the value has changed? Is it stored somewhere? And the reason I ask: can I replicate that behaviour?

Thanks

Heathcliff
  • 3,048
  • 4
  • 25
  • 44

2 Answers2

0

I think it stores the original value and compares current value with original one on blur.

If by replicate you mean trigger the event then you can read here: http://api.jquery.com/trigger/

Ilia Frenkel
  • 1,969
  • 13
  • 19
0

This is resolved by the browser. You can also trigger for values changed on text inputs by attaching a handler to the onchange event:

<input onchange="alert('value changed!');" />

And also in Javascript:

document.getElementById("my_text_input").onchange = function(){
    alert("value changed!");
}

Of course you can replicate this behaviour. You can try it yourself here.

[Edit]
You have the onblur event, which fires every time the user leaves the control.
You could keep track of the input value, or set a flag to be accesible for both change and blur events:

(function(){
    var changed = false;

    document.getElementById("my_text_input").onchange = function(){
        changed = true;
    }
    document.getElementById("my_text_input").onblur = function(){
        if (!changed) {
            //Your logic here
        }
        changed = false; //set it back to false for next loss of focus
    }
})();
Pablo
  • 360
  • 3
  • 13
  • Yes, but the "onchange" event is fired _whenever_ the input is changed, not if the content changes (so for instance, if inside the input you had "string" and then you erase it and rewrite "string", the event is triggered). Autocomplete's version doesn't do that. When does it check if the value is the same? Because otherwise, the change event isn't fired. – Heathcliff Sep 13 '12 at 13:35
  • You are wrong, it won't fire if you rewrite the same value. try it yourself http://jsfiddle.net/rWzqJ/ – Pablo Sep 13 '12 at 14:44
  • True, my bad. Still, I have the same question (please, see my comment in the first post to Dipaks): is there a way to turn it in a boolean value? Something like $(this).isChanged(), or something. – Heathcliff Sep 13 '12 at 14:54