Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change-and-then-blur event

We have one of those scenarios where you prefer an event to be triggered only when you leave the input field. That is because the event leads to a "postback" like ajax request which in turn updates the input fields. A change event would start an infinite loop.

On the other end, a blur event is triggered each time the user leaves the field regardless whether the field was changed or not.

What I am looking for is kind of a "change-and-then-blur" event which fires if the field changed but only when it is left (blurred).

I reckon jQuery does not have that out of the box. Is there any easy way to extend it for that?

Update - I have just tried hard to reproduce the issue of the infinite loop in a simple jsfiddle - to no avail.

There is knockout involved as well as jQuery autoNumeric and a custom autoNumeric knockout binding. The values are updated using ko.mapping. But even when including all of those my jsfiddle seems to work perfectly with the simple jQuery change event: http://jsfiddle.net/a1tbxnpp/

So I guess I will rather investigate the issue with my application that bother introducing another jQuery event.

like image 830
chiccodoro Avatar asked Oct 20 '22 23:10

chiccodoro


1 Answers

Why not using focus and blur. On focus save the value of the input field in a variable. On blur compare the value of the input with the variable, if it's different do the ajax call.

like image 115
Mathieu Labrie Parent Avatar answered Nov 02 '22 07:11

Mathieu Labrie Parent