Monday, February 6, 2012

How to find parent element's id using jquery

Here I am explaining how to find the parent element's id of a html element using jquery function.


$(this).parent().attr(''id);

this jquery function will give the "id" of the given element's parent  element.


eg:


<html>
   <script>
      $("a").click(function(){
         var parentid = $(this).parent().attr(''id");
      });
   </script>
   <body>
     <div id="parent-id" class="parent-class">
       <a href="#">test</a>
     </div>
  </body>
</html>


the variable "parentid" will return the value parent-id


0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Maintained by Web Themes