Bind Multiple Events Simultaneously with jQuery

It is sometimes necessary to bind two or more events to an element that utilize the same function. Using jQuery it might look something like this (where 'fn' is a function reference).

  1. $("a")
  2.     .bind("focus", fn)
  3.     .bind("mouseover", fn);

That isn't terrible but it would be nice if we could write it like this.

  1. $("a").bind("focus mouseover", fn);

Well, now we can! I just checked in an extension to the jQuery plugins repository that adds this functionality. It is pretty small and if you use this pattern often, then save yourself some typing and get this extension! You can grab the source code from SVN or from here. You can find a test/example page here. This new syntax also applies to the one and unbind methods as well.

2 Responses to “Bind Multiple Events Simultaneously with jQuery”

  1. Nicolae Namolovan Says:

    Was looking for this, thanks for pointing.

  2. Cherry Says:

    THANK YOU Brandon !!!!!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>