Skip to content
Related Articles

Related Articles

jQuery :root Selector

Improve Article
Save Article
  • Last Updated : 17 Nov, 2022
Improve Article
Save Article

The jQuery :root selector is used to select the root element of the HTML document. 

Syntax:

$(":root")

Parameter: This selector contains single parameter root which is the root element of document. 

Example 1: This example use :root selector to select document and set background-color to green. 

HTML




<!DOCTYPE html>
<html>
   
<head>
    <title>
        jQuery :root Selector
    </title>
    <script src=
    </script>
</head>
 
<body style="text-align:center;">
    <h1>GeeksForGeeks</h1>
    <h2>jQuery :root Selector</h2>
   
    <!-- Script uses :root selector -->
    <script>
        $(document).ready(function () {
            $(":root").css("background-color", "green");
        });
    </script>
</body>
   
</html>


Output:

 

Example 2: This example use :root selector to select document and change text color to red. 

HTML




<!DOCTYPE html>
<html>
   
<head>
    <title>
        jQuery :root Selector
    </title>
    <script src=
    </script>
</head>
 
<body style="text-align:center;">
    <h1>GeeksForGeeks</h1>
    <h2>jQuery :root Selector</h2>
    <button>Change color</button>
   
    <!-- Script uses :root selector -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $(":root").css("color", "red");
            });
        });
    </script>
</body>
   
</html>


Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!