Sebastien
Sebastien

Reputation: 2679

Bind element and its children in jQuery

I'd like to bind an event to an element and its children. What is the best way to do this?

$(element).bind('click', function(event) {
    doSomething();
});

Upvotes: 4

Views: 6112

Answers (2)

Jishnu A P
Jishnu A P

Reputation: 14390

$(element).bind('click', function(event) {
    doSomething();
}).children().bind("click",function(event){
    // code to handle children click here
    event.stopPropagation(); // if you don't want event to bubble up
});

Upvotes: 5

Alnitak
Alnitak

Reputation: 339957

The code you have will do just that.

Look in the event's target field to find out which actual child node saw the event.

Upvotes: 0

Related Questions