.parent() - Get the parent of element jQuery

Get the parent of element
In web applications while working with clientside we may need to get parent element of elected element. jQuery provides function .parent() that we can use to easily get parent of the element.

Here we will see example where we have link inside div and on clicking on link we will get it's parent div.

Code to get parent of element [jQuery]

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <title>Get parent of element</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#submitbtn").click(function () {
                var parent = $(this).parent();
                alert(parent.text());
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="border:1px blue solid; background-color:seashell; width:200px; height:100px;">
       This is parent
       <br/>
       <input type="button" id="submitbtn" value="Click Me"/>
    </div>
    </form>
</body>
</html>

0 Comments :

Post a Comment