I have the following html code:
<input type="text" id="theInput" value=""/>
<a href="#" id="theLink">Click me</a>
I want to detect when the input changes and perform an operation in this case, but ONLY when the user has not clicked in the link. I have tried this:
$('#theLink').live('click', function(){
alert('click');
});
$('#theInput').live('change', function(){
alert('change');
});
However change is always executed before click when the value in the input changed, due to Javascript event precedence rules, and therefore only “change” message is displayed.
I would like it to display change only if the input value changed and the user exited the input clicking in any other place instead of the link. In that last case I would like to display click.
The example is here.
I use jQuery 1.6.4.
As far as I know, the
clickevent fires after theblurandchangeevents in every browser (have a look at this JSFiddle). The order ofblurandchangeis different across browsers (source: Nicholas Zakas).To solve your problem, you could listen to
clickevents on the document and compare the event’s target with#theLink. Any click event will bubble up to the document (unless it is prevented).Try this:
JSFiddle: http://jsfiddle.net/PPvG/TTwEG/