views:

17

answers:

2

I want a click on a span tag with .myClass ,and all its descendants, to do something..

$('.myClass'). *<all elements below .myClass>*.click(function(){
//do something
});

How do i select all elements below the .myClass selector ? Not only children, but every node below them aswell.

Im in IE7

+3  A: 

A click by default will bubble up to the parents, so you just need:

$('.myClass').click(function(){
  //do something
});

If you really do need all elements, use $('.myClass *'), but typically you want to stay away from this, event bubbling is much more efficient and happens by default. If you need the target, seeing which actual child it came from use event.target, like this:

$('.myClass').click(function(e){
  var clickedDOMElement = e.target;
  //do something
});
Nick Craver
A: 

If you want to do this explicitly:

$('.myClass').find('*').click(blah blah..);

Documentation: http://api.jquery.com/find/

Andrew
Actually, sorry, there is a difference between this and what you want to do - this won't listen for the click event on the .myClass itself, *only* the children.
Andrew
As I said in the answer above...you'll want to stay away from actually doing this in almost every scenario, image for a moment `$('body').find('*').click(...)`, it's binding who knows how many event handlers...just not a good way to approach the problem.
Nick Craver