Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery :root Selector

Posted in jQuery Tutorial
Updated on Oct 30, 2024
By Mari Selvan
👁️ 39 - Views
⏳ 4 mins
💬 1 Comment
jQuery :root Selector

Photo Credit to CodeToFun

🙋 Introduction

jQuery is renowned for its simplicity and effectiveness in web development. Among its arsenal of selectors is the :root selector, which targets the root element of the document. Understanding how to leverage this selector can enhance your ability to manipulate the DOM (Document Object Model) effectively.

In this guide, we'll delve into the usage of the jQuery :root selector with clear examples to help you harness its power.

🧠 Understanding :root Selector

The :root selector is used to target the root element of the document, typically the <html> element. It's particularly useful when you want to apply styles or manipulate attributes that affect the entire document.

💡 Syntax

The syntax for the :root selector is straightforward:

syntax.js
Copied
Copy To Clipboard
$(":root")

📝 Example

  1. Changing Document-Wide Styles:

    Suppose you want to change the font family of the entire document. You can achieve this using the :root selector along with CSS:

    example.js
    Copied
    Copy To Clipboard
    $(":root").css("font-family", "Arial, sans-serif");

    This will set the font family of the root element (usually <html>) and consequently affect the entire document.

  2. Setting Document-Level Variables:

    You can use the :root selector to set and access CSS variables at the document level. For example:

    index.html
    Copied
    Copy To Clipboard
    :root {
      --main-color: #3498db;
    }
    example.js
    Copied
    Copy To Clipboard
    $(":root").css("--main-color", "#ff0000");

    This will change the value of the --main-color variable to red (hex code #ff0000) throughout the document.

  3. Applying Document-Wide Event Handlers:

    You can bind event handlers to the root element to handle events globally across the document. For instance:

    example.js
    Copied
    Copy To Clipboard
    $(":root").on("click", function() {
      alert("Document clicked!");
    });

    This will trigger an alert whenever any part of the document is clicked.

  4. Accessing Document-Level Data Attributes:

    The :root selector can be useful for accessing data attributes defined at the document level. For example:

    index.html
    Copied
    Copy To Clipboard
    <html data-theme="light">
    example.js
    Copied
    Copy To Clipboard
    var theme = $(":root").data("theme");
    console.log("Document theme:", theme);

    This will log the value of the data-theme attribute defined on the root element.

🎉 Conclusion

The jQuery :root selector provides a convenient way to target and manipulate the root element of the document. Whether you need to apply document-wide styles, set variables, handle events globally, or access document-level data, this selector offers a straightforward solution.

By mastering its usage, you can efficiently manage and manipulate the entire document structure with ease.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy