ARTICLE

How to use jQuey Parent Method in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET March 01, 2011
The jQuery parent() method is used to get the ancestors of each element in the DOM tree through the current set of matched elements.
Download Files:
 
Reader Level:

As you know the terms parent, child, and sibling are used to describe the relationships. Parent nodes have children and children on the same level are called siblings. The jQuery parent() method is used to get the ancestors of each element in the DOM tree through the current set of matched elements.


This method optionally accepts a selector expression of same type which we pass to the
$() function. The elements will be filtered by testing whether they match it or not if the selector is supplied.

Example

Here is an example to find out all the unique div parent elements of each span.

<html>
<head><title>jQuery Parent Selector</title>
 <style> 
    p, div, span {margin:2px; padding:1px; } 
    div { border:2px white solid; } 
    span { cursor:pointer; font-size:12px; }
    .selected { color:red; }
    b { color:blue; display:block; font-size:14px; } 
     .style1
     {
        color: #33CC33;
     }
  </style>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
  <body style="background-color: #F7F7F7"> 
    <p>  
      <div style="font-family: Verdana; font-size: small">   
        <div><span>Hello!</span></div>
           <span>Welcome to vbdotnetheaven,</span>
      </div>
      <div style="font-family: Verdana; font-size: small">    
           <span>Share your ideas with us.</span>
      </div>
    </p>
      <b class="style1" 
         style="font-family: Verdana; font-size: small; font-weight: bold">Click on 
           each statement to toggle their parents.</b>
  <script>
     function showParents() {
        $("div").css("border-color", "white");
          var len = $("span.selected")
                           .parents("div")
                           .css("border", "2px blue solid")
                           .length;
          $("b").text("Number of unique div parents are: " + len);
        } 
        $("span").click(function () {
          $(this).toggleClass("selected"); 
          showParents();
     });
</script>
</body>
</html>

Output Result

parent1.gif

Now click on the first word to find out all unique div parent elements of each span.

parent2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor