parent, parentsUntil, closest, offsetParent Methods in jQuery Ancestor Traversing Methods

 <!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/parent_parentsUntil_closest_offsetParent.css">
    <title>parent, parentsUntil, closest, offsetParent</title>
</head>

<body>
    <h1>jQuery Ancestor Methods</h1>
    <div id="main-outer" style="position:relative">
        <h2>Main Outer</h2>
        <div id="outer" style="position:relative">
            <h2>Outer</h2>
            <div id="inner">
                <h2 id="child-head">Inner</h2>
                <div class="box">A</div>
                <div class="box">B</div>
                <div class="box" id="child-C">C</div>
                <div class="box">D</div>
            </div>
        </div>
    </div>
    <script src="js/jquery-3.7.1.min.js"></script>
    <script>
        // jQuery DOM Traversing Methods : Ancestors Methods, Descendants Methods, Siblings Methods, Filtering Methods
        // jQuery Ancestor Traversing Methods : parent(),parents(),parentsUntil(),closest(),offsetParent()
        $(document).ready(function () {
            // $("#child-C").parent().css("background", "#ff0000");
            // $("#inner").parent().css("background","#ff0000");

            // $("#child-C").parents().css("background","#ff0000");
            // $("#inner").parents("#main-outer").css("background","#ff0000");
            // $("#child-C").parentsUntil("#main-outer").css("background","#ff0000");
            // $("#inner").parentsUntil("#main-outer").css("background","#ff0000");
            // $("#inner").offsetParent().css("background","#ff0000");
            // $("#child-C").offsetParent().css("background","#ff0000");
            //offsetParent() will target only those properties which have first "position:relative" propety(Not remaining parent though they have "position:relative" property)

            $("#child-C").closest("div").css("background", "#ff0000");   //closest() will target only nearest parent which is given in parameter(Here, "div")
        });
    </script>
</body>

</html>










Comments

Popular posts from this blog

Basic Syntax in jQuery

Event "Target" Property in jQuery