Small Mistake When Simultaneously Binding Multiple Events With jQuery

Posted June 8, 2012 at 10:05 AM by Ben Nadel

Tags: Javascript / DHTML

For some reason, on my latest project, I keep making a mistake when simultaneously binding multiple events on a given jQuery collection. The annoying part of the mistake is that it's not terribly easy to track down as it doesn't throw an error - it simply "fails" silently. To demonstrate, here's the kind of code that I keep writing:

  • <!doctype>
  • <html>
  • <head>
  • <title>Event Binding / Unbind Mistake</title>
  • <script type="text/javascript" src="./jquery-1.7.1.js"></script>
  • <script type="text/javascript">
  •  
  •  
  • // WRONG: Bind two different events.
  • $( document ).bind(
  • "open, close",
  • function( event ){
  •  
  • console.log( "Triggered: " + event.type );
  •  
  • }
  • );
  •  
  • // Trigger both events.
  • $( document ).triggerHandler( "open" );
  • $( document ).triggerHandler( "close" );
  •  
  •  
  • </script>
  • </head>
  • <body>
  • <!-- Left intentionally blank. -->
  • </body>
  • </html>

As you can see, I am attempting to bind both the "open" and "close" event types to the same handler. And, when I run the above code, I get the following output:

Triggered: close

There were no errors; but, only one of the events was captured. The reason for this - the error that I keep making - is the way I am listing my event types:

"open, close"

See the problem? It's the comma! jQuery expects a space-delimited list of event types. So, when it parses the input, it sees the following two events:

  • open,
  • close

Notice that the "," is actually part of the first event type. Removing the comma and everything works perfectly.

Now, if only I can get this irksome habit out of my head!




Reader Comments

Jun 8, 2012 at 11:10 AM // reply »
10 Comments

I agree that that is counter-intuitive, especially since a list of multiple selectors is comma-delimited. Seems to be inconsistent.

"bind" is deprecated in favor of "on", but "on" uses that same syntax.

$( document ).on("open close"...


Jun 8, 2012 at 11:12 AM // reply »
11,238 Comments

@Joel,

I use on() when I can, but the project where this keeps popping up is using an older version of jQuery. Soon to be updated. Hopefully now that I've put it on paper, it'll get out of my head :D


Jun 8, 2012 at 2:59 PM // reply »
1 Comments

What about:

  • function eventHandler(ev){
  • //do cool stuff here
  • }
  • $(document).bind({
  • open: eventHandler,
  • close: eventHandler
  • });

More verbose, but less error prone.


Jul 2, 2012 at 9:41 AM // reply »
11,238 Comments

@Sergio,

Good point. I have not yet gotten used to the hash-based configuration for functions in the newer jQuery releases (ex. multi-bindings, defining attributes of a new DOM element).

I think I need to go back through the jQuery API and really learn the new variations of the method calls.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools