0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
stratis
  • 7,750
  • 13
  • 53
  • 94
  • 1
    a simple debounce would solve this, and should be used regardless since this is an ajax request based on user input events. – Kevin B Sep 09 '16 at 20:46
  • http://stackoverflow.com/questions/5963669/whats-the-difference-between-event-stoppropagation-and-event-preventdefault – Malk Sep 09 '16 at 20:47
  • @KevinB what's a simple debounce? – stratis Sep 09 '16 at 20:51
  • google debounce. the first 4 results i see (w3schools being blocked) have pretty good explanations and examples. – Kevin B Sep 09 '16 at 20:52
  • @KevinB Yes. The debounce function will indeed work. However, it is but a mere workaround "hacked" for this case. Is there a more "proper" way to deal with this issue? – stratis Sep 09 '16 at 21:02
  • how is that a hacked workaround? it simply delays the request from happening to prevent 50 requests being sent when the user clicks the button 50 times in a row. It's a very common technique for this sort of thing and not at all a "hack" – Kevin B Sep 09 '16 at 21:03
  • @KevinB In the scenario you mention it's not because you prevent the mindless user from doing "harm". However using debounce to camouflage erroneous programming constructs (like mine) may not be the best way to go. – stratis Sep 09 '16 at 21:15
  • just curious why you're not using keyup or input event for `#searchArea` – yezzz Sep 09 '16 at 21:15
  • @yezzz traffic throttling... – stratis Sep 09 '16 at 21:16
  • @kstratis heh, and that's what a debounce is for. so not only will you get a more responsive form, you'll prevent the user from accidentally spamming your server. – Kevin B Sep 09 '16 at 22:06
  • @kstratis: Understood. And the change event was added for better UX? Also, what about blur event on the input, allowing to use relatedTarget: https://jsfiddle.net/kd2nm9db/ – yezzz Sep 09 '16 at 22:24
  • 1
    @Kevin B: thanks for the insight about debouncing – yezzz Sep 09 '16 at 22:24

0 Answers0