Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Sir,


I Have multiple DIV on same class name, Here is the sample Code.

HTML
<div id = "1" class ="Item">A</div>
<div id = "2" class ="Item">B</div>
<div id = "3" class ="Item">C</div>
<div id = "4" class ="Item">D</div>


I need javascript for div click event and in that event i need to get the Div ID using Div class,

For eg: When i clicked the Second Item Class Name = Item , then i got the ID as "2"


Please Help Me.

Thanks,

Dileep..
Posted
Updated 21-Aug-12 23:24pm
v2

1 solution

You can do that by attaching an onclick handler to each of the divs. When it's called, the div element is passed to the handler/ From there, it's a simple matter of returning the id of the element that called the myOnclick function.

JavaScript
function myOnclick(el)
{
 alert(el.id);
}


HTML
<div id = "1" onclick='myOnclick(this);' class ="Item">A</div>
<div id = "2" onclick='myOnclick(this);' class ="Item">B</div>
<div id = "3"  onclick='myOnclick(this);'class ="Item">C</div>
<div id = "4"  onclick='myOnclick(this);'class ="Item">D</div>
 
Share this answer
 
Comments
dilzz 22-Aug-12 5:34am    
Thank you sir,

I have been searching for this long time.... thanks a lot... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900