Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 03, 2012 03:49 AM by mahedee
Member
45 Points
50 Posts
May 01, 2012 04:07 PM|LINK
Hi all,
Good day.
I have a question on jquery accessing the parent div.
Let's say
<div id="d1"> <div id="c1"> <div ="b1"> <div id="a1"> </div> <div id="a2"> </div> </div> </div> </div>
when i click div with an id a2 i want to get the div id c1
thanks in advance
jhon
JQuery
Contributor
5131 Points
827 Posts
May 01, 2012 04:09 PM|LINK
Try this
var id = $("#a2").closest("div").attr("id");
May 01, 2012 04:16 PM|LINK
already tried that...
Participant
778 Points
225 Posts
May 01, 2012 04:36 PM|LINK
http://api.jquery.com/click/ & http://api.jquery.com/get/
Star
10411 Points
1196 Posts
May 03, 2012 02:32 AM|LINK
Hi, you can use “.parent()” method to get the parent element, for example:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#a2').click(function () { var div = $(this).parent().parent(); alert($(div).attr('id')); }); }); </script> </head> <body> <form id="form1" runat="server"> <div id="d1">d1 <div id="c1">c1 <div id="b1">b1 <div id="a1">a1 </div> <div id="a2">a2 </div> </div> </div> </div> </form> </body> </html>
450 Points
116 Posts
May 03, 2012 03:49 AM|LINK
Try with this.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type = "text/javascript" src = "Scripts/jquery-1.4.1.min.js"></script> <script type = "text/javascript"> $(document).ready(function () { $('#a2').click(function () { var div = $(this).parent().parent(); alert($(div).attr('id')); }); }); </script> </head> <body> <form id="form1" runat="server"> <div id="d1">Div - d1 <div id="c1">Div -c1 <div ="b1">Div -b1 <div id="a1"> Div -a1 </div> <div id="a2"> Div -a2 </div> </div> </div> </div> </form> </body> </html>
Keep it mind that you want to get the id of parent of parent. So use $(this).parent().parent();
mariasophiai...
Member
45 Points
50 Posts
jquery
May 01, 2012 04:07 PM|LINK
Hi all,
Good day.
I have a question on jquery accessing the parent div.
Let's say
<div id="d1">
<div id="c1">
<div ="b1">
<div id="a1">
</div>
<div id="a2">
</div>
</div>
</div>
</div>
when i click div with an id a2 i want to get the div id c1
thanks in advance
jhon
JQuery
Ramesh T
Contributor
5131 Points
827 Posts
Re: jquery
May 01, 2012 04:09 PM|LINK
Try this
var id = $("#a2").closest("div").attr("id");mariasophiai...
Member
45 Points
50 Posts
Re: jquery
May 01, 2012 04:16 PM|LINK
already tried that...
MahadPK
Participant
778 Points
225 Posts
Re: jquery
May 01, 2012 04:36 PM|LINK
Try this
http://api.jquery.com/click/ & http://api.jquery.com/get/
Allen Li - M...
Star
10411 Points
1196 Posts
Re: jquery
May 03, 2012 02:32 AM|LINK
Hi, you can use “.parent()” method to get the parent element, for example:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#a2').click(function () { var div = $(this).parent().parent(); alert($(div).attr('id')); }); }); </script> </head> <body> <form id="form1" runat="server"> <div id="d1">d1 <div id="c1">c1 <div id="b1">b1 <div id="a1">a1 </div> <div id="a2">a2 </div> </div> </div> </div> </form> </body> </html>If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
mahedee
Member
450 Points
116 Posts
Re: jquery
May 03, 2012 03:49 AM|LINK
Try with this.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type = "text/javascript" src = "Scripts/jquery-1.4.1.min.js"></script> <script type = "text/javascript"> $(document).ready(function () { $('#a2').click(function () { var div = $(this).parent().parent(); alert($(div).attr('id')); }); }); </script> </head> <body> <form id="form1" runat="server"> <div id="d1">Div - d1 <div id="c1">Div -c1 <div ="b1">Div -b1 <div id="a1"> Div -a1 </div> <div id="a2"> Div -a2 </div> </div> </div> </div> </form> </body> </html>Keep it mind that you want to get the id of parent of parent. So use $(this).parent().parent();
JQuery
Mahedee
Blog: http://mahedee.blogspot.com