0

so I currently wanted to bind two same event handlers(click), that are in different files, to one button element in my html but the second event handler isnt printing any of my debug statements to the console.

HTML:

<form>
  <input type="button" value="Find it!" id="submit-button">
</form>

foobar.js:(this successfully triggers my google maps)

$("#submit-button").on("click", function(){
    google.maps.event.trigger(map,'resize');
});

utility.js: (this event handler doesnt do anything):

    $("#submit-button").on("click", function(){
      console.log($('#termText').val());
    });

I've read from other posts that JQuery should let you bind two event handlers to the same element. But strangely it isnt printing anything for my second click (in utility.js)

exAns
  • 93
  • 1
  • 2
  • 10
  • Yes, you can have multiple handlers like this. Make sure you're running all the code in `$(document).ready()`. – Barmar Dec 15 '16 at 21:19
  • You're including both JS files right? – Fueled By Coffee Dec 15 '16 at 21:19
  • Does the second one work if you don't do the first one? – Barmar Dec 15 '16 at 21:20
  • yup! sorry about that. I have both files linked to the html doc @ac – exAns Dec 15 '16 at 21:21
  • @exAns - before or after the form html? – Igor Dec 15 '16 at 21:22
  • @Barmar hm nope it doesnt. I tried commenting out the first one and keeping the second one but still seems like its not working – exAns Dec 15 '16 at 21:24
  • i have my scripts in the head tag (not body) – exAns Dec 15 '16 at 21:25
  • Does #termText have a valid val()? Can it just print a string? – Mario Wenig Dec 15 '16 at 21:26
  • 1
    @exAns - then they should both come after jQuery js file and the code fragments you showed must be wrapped in `$(document).ready()` – Igor Dec 15 '16 at 21:27
  • 1
    Either put your scripts at the end of the body, or use `$(document).ready()`. The problem is you're binding the handlers before the DOM element exists. – Barmar Dec 15 '16 at 21:27
  • that makes sense haha sweet! thanks @i – exAns Dec 15 '16 at 21:44
  • @Igor if i wanted to hold a particular order, is there a way i can do that with built-in functionalities from jQuery ? – exAns Dec 15 '16 at 22:22
  • 1
    "particular order" of what? the order of your two files does not matter (as long as there is no dependency of one on the other). But anything that uses jQuery (or its `$` alias) must come after the `script` tag that links in jQuery javascript. – Igor Dec 15 '16 at 22:37

0 Answers0