I have a dead simple search form:
<div>
<input id="searchArea" type="text" value="" placeholder="Enter your search terms">
<button id="searchButton">Search</button>
</div>
and a snippet of Javascript controlling it:
function searchFunction() {
console.log("This is a POST request being send to the server");
}
$("#searchButton").on("click", function(e) {
e.preventDefault();
searchFunction();
})
$("#searchArea").on("change", function(e) {
e.preventDefault();
searchFunction();
});
The problem is that click and change overlap in functionality and so I get the following:
When I type something in the text box and then click elsewhere in the screen, searchFunction
is fired properly but if "elsewhere" becomes the search button, then searchFunction
double fires.
My question is this: Once inside the click
handler, is there any way to cancel the change
handler from firing as well which would otherwise cause searchFunction
to double fire?
Mind that searchArea
and searchButton
don't have a parent-child relationship which means conventional methods like preventDefault
and stopPropagation
won't work.
You can see it in action in this fiddle.