Knowledge Walls
Ammu
Hyderabad, Andhra Pradesh, India
Passcode:
JQuery Traversing - Ancestors in JQuery of JQuery
908 Views

Traversing Up the DOM Tree

Three useful jQuery methods for traversing up the DOM tree are:

  • parent()
  • parents()
  • parentsUntil()
example
<!DOCTYPE html>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title>internal</title>
        <script   src="https://code.jquery.com/jquery-3.4.1.min.js"   integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="   crossorigin="anonymous"></script>
    </head>
    <body>
        <script>
            $(document).ready(function(){
                $("#btn").click(function(){
                    $("#ul1").parent().css("background-color","red");
                    $("#ul1").parents("div").css("color","yellow");
                     $("#ul1").parentsUntil("div.div1").css("color","blue");
                });
            });    
                
        </script>
        <div width="500" height="300" style="background-color: orange;">
            hello
        <div id="div1">
            <ul id="ul1">
                <li>1</li>
                <li>2</li>
            </ul>
            <button id="btn">click here for parent</button>
        </div>
        </div>
    </body>
</html>
Previous Topics
Previous lessons of current book.
JQuery of JQuery
JQuery of JQuery
Best Lessons of "JQuery"
Top lessons which are viewed more times.
JQuery of JQuery
JQuery of JQuery
JQuery of JQuery
JQuery of JQuery
JQuery of JQuery
JQuery of JQuery
JQuery of JQuery
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details