4

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();
});
Sebastien
  • 2,607
  • 9
  • 30
  • 40

2 Answers2

5
$(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
});
Jishnu A P
  • 14,202
  • 8
  • 40
  • 50
0

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495