2

I have two elements and I want to delegate click event to their children. This code works:

list.on('click', '> *', function (e) {
    clickHandler($(this), e);
});
listSelected.on('click', '> *', function (e) {
    clickHandler($(this), e);
});

Tried to use jQuery element array, but this code doesn't work:

$([list, listSelected]).on('click', '> *', function (e) {
    clickHandler($(this), e);
});
3
  • list and listSelected have to be DOM elements, not jQuery elements in order to pass as an array of selectors Commented Jan 9, 2015 at 19:18
  • @Teemu - accoring to documentation this is called delegated event or do I miss something? http://api.jquery.com/on/ Commented Jan 9, 2015 at 19:24
  • @iGidas Looks like I've missed the fact, that you can ofcourse delegate to many elements too : ). Commented Jan 9, 2015 at 19:31

2 Answers 2

2

You can use add()

list.add(listSelected).on('click', '> *', function (e) {
    clickHandler($(this), e);
});
Sign up to request clarification or add additional context in comments.

Comments

0
$('#your_list').children().each(function(){
    $(this).bind('click', clickHandler) ;
}) ;

1 Comment

Note: you should use .on() instead of bind as of jQuery 1.7+

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.