Do not fire one event if already fired another
javascript, jquery, jquery-events
Solution
What happens is that onChange fires when the focus leaves the #input. In your case, this coincides with clicking on the button. Try pressing Tab, THEN clicking on the button.
To handle this particular case, one solution is to delay the call to the `change` event enough check if the button got clicked in the meantime. In practice 100 milisecond worked. Here's the code:
$().ready(function() {
var stopTheChangeBecauseTheButtonWasClicked = false;
$('#button').on('click', function(e) {
stopTheChangeBecauseTheButtonWasClicked = true;
$('#wtf').html("I don't need to change #input in this case");
});
$('#input').on('change', function(e) {
var self = this;
setTimeout(function doTheChange() {
if (!stopTheChangeBecauseTheButtonWasClicked) {
$(self).val($(self).val() + ' - changed!');
} else {
stopTheChangeBecauseTheButtonWasClicked = false;
}
}, 100);
});
});
And the fiddle - http://jsfiddle.net/dandv/QhXyj/11/
Problem
I have a code like this: ``` $('#foo').on('click', function(e) { //do something }); $('form input').on('change', function(e) { //do some other things )); ``` First and second events do actually the same things with the same input field, but in different way. The problem is, that when I click the `#foo` element - form change element fires as well. I need form change to fire always when the content of input is changing, but not when `#foo` element is clicked. That's the question )). How to do this? Here is the code on jsfiddle: http://jsfiddle.net/QhXyj/1/