Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  156] [ 2]  / answers: 1 / hits: 40845  / 10 Years ago, wed, july 2, 2014, 12:00:00

How can i get Parent Div id of component.



I have already referred this link but its not working.



jQuery get id of element by searching for it by class



Here Is my Sample Code.



<html>
<head>
<script src=http://code.jquery.com/jquery-latest.min.js></script>
<script>
function linkClickEvent()
{
alert ($(this).parent().attr('id')); //Gives Me Undefined
alert($(this).closest('div').attr('id')); //Gives Me Undefined
alert($(this).parent().attr('id')); //Gives Me Undefined
}
</script>
</head>
<body>
<div id=container_1>
<a href=# onclick=linkClickEvent(); id=link_1>Container_1</a>
</div>
<div id=container_2>
<a href=# onclick=linkClickEvent(); id=link_2>Container_2</a>
</div>
<div id=container_3>
<a href=# onclick=linkClickEvent(); id=link_3>Container_3</a>
</div>
</body>
</html>

More From » jquery

 Answers
11

Change your onclick event, in onclick event function linkClickEvent pass current element reference using this :



 <a href=# onclick=linkClickEvent(this); id=link_1>Container_1</a>


and in function add parameter to accept element reference:



function linkClickEvent (element) {
console.log($(element).closest(div).attr(id));
}

[#70348] Monday, June 30, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;