jQuery - how to assign an event to a group of controls with a common parent

plq
plq used Ask the Experts™
on
Hi Folks.

I've got a html structure like this:

<div id="topdiv">
<table><tr>
<asp:placeholder id="content">
</tr>
</table>
</div>

The placeholder is populated with <td> elements and each one of those contains a structure in order of td > div > table > tr > td > div.

At that lowest level div, I want to assign a jquery click event. How could I do this in jquery code ?

I can of course lose the table elements and move to semantic html, but conceptually I just need to know how to assign an event to a group of elements, all with different ids, where they have a common parent.

thanks

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Research & Development Manager
Commented:
$('#topdiv table tr td div table tr td div').onclick(function(){alert('You clicked a div element');});
plq

Author

Commented:
I extended what you said to this

$('#topdiv table tr td div table tr td div').onclick(function(event){var target = $(event.target); alert('You clicked a div element ' + target.attr("id"));});

target gives me the element that was clicked. So instead of adding an event to every element, I can capture it at the top level and it still gives me the lowest level element which is pretty cool..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial