3

I am looking for a better way to bind multiple events to a single element in jQuery. I am trying to avoid writing multiple $(element).bind('event', ...) or $(element).event(...) statements.

Code

// old way    
var textbox = $('input');
$(textbox).focus(function() { ... }
$(textbox).blur(function() { ... }

// new way
$(textbox).extend({
    focus: function() {
           ...
    },
    blur: function() {
            ....
    }
});

Unfortunately, this implementation is not working. Does anyone have a better suggestion? Thank you.

2
  • 1
    Take a look at .on(). Commented Mar 19, 2012 at 14:52
  • 1
    @AbeMiessler, his "new way" it is not working because he's trying to extend the jQuery object with methods named after the events. However, this won't bind the events. Commented Mar 19, 2012 at 14:53

7 Answers 7

9

All of the answers so far assume you want to bind the same callback function to multiple events. If that's not the case, consider using .on() with an event map:

$('selector').on({
    focus: function(e) {
        // do something for focus
    },
    blur: function(e) {
        // do something else entirely for blur
    },
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, several answer explained how to do this (including mine), just in a different way (using e.type). This is actually my preferred method so that common elements or logic can be easily shared between the handlers.
3

Try this:

$("textbox").bind('focus blur', function() {
    // your code
});

For jQuery 1.7+ bind has been superceded by on:

$("textbox").on('focus blur', function() {
    // your code
});

In both of these cases, the function specified will be run on all events listed in the first parameter.

Comments

3

Use jQuery's .on() method:

$('input').on("focus blur", function () {
});

If you need to execute conditional logic based on the event:

$('input').on("focus blur", function (e) {
    var whichEvent = e.type; // Will be "focus" or "blur"
});

Comments

0

You can use

<element>.on("eventhandlers as commaseparated list",function(){})

if you can use one function for all those handlers, or

element.click(...)
       .<anotherhandler>(...)
       .<yetanother>(...)

if you need different functions.

.on()is the preferred way though.

Comments

0
// My way
var textbox = $('input');
$(textbox).on('focus blur', function(e){
  if (e.type == 'focus'){
    // do the focus stuff
  } else if (e.type == 'blur'){
    // do the blur stuff
  }
}

This is untested, but the principle holds

Comments

0

You can make you use of bind function in jquery:

Ex:

$(textbox).bind('focus blur',function(){
  //do something
 });

Comments

0

Once you've saved a jQuery object in a variable, you don't need to keep converting it to a jQuery object over and over. You can also "chain" your event bindings, since they return the original object.

Try something like this:

var $textbox = $('input');  // (Use a $ to mark variables that hold jQuery objects
$textbox
    .on("focus", function() { ... })
    .on("blur", function() { ... });

(Also, make sure you check that you're using the right event names... I have no idea how much time I've wasted hunting bugs that were because I made up my own name for an event.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.