finding clicked id deep in document using jquery
Question
I'm going to use the text from the jquery example, For example, consider the HTML:
<lots of divs to get to here>
<div id="#targetid_0">
Click here
</div>
<div id="#targetid_1">
Trigger the handler
</div>
I have a series of questions that are plaguing me...
Assuming that I click on 'Click Here' or 'Trigger the Handler':
- If I'm assigning a value via the attr(targetid_x,JSON.ID), how can I use alert to show me that value? It's driving me nuts!
- How do I find out the specific clicked #tag? (sort of related to question 1).
I'd like to see if this can be accomplished with Event Delegation or at least without classes.
Halp!
Solution
$('div').click(function() {
$(this).attr('id');
});
OTHER TIPS
Try this:
$('div[id]').click(function(){
alert(this.id);
return false;
});
This adds an event listener to all divs that have an id.
The return false;
part stops propagation. Thus if you have nested divs that have id's only the bottom(inner) one will show the alert, and then stops the event bubbling.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow