jQuery Basic
jQuery Selectors
- jQuery Selectors
- jQuery *
- jQuery :animated
- jQuery [name|=”value”]
- jQuery [name*=”value”]
- jQuery [name~=”value”]
- jQuery [name$=”value”]
- jQuery [name=”value”]
- jQuery [name!=”value”]
- jQuery [name^=”value”]
- jQuery :button
- jQuery :checkbox
- jQuery :checked
- jQuery Child Selector
- jQuery .class
- jQuery :contains()
- jQuery Descendant Selector
- jQuery :disabled
- jQuery Element
- jQuery :empty
- jQuery :enabled
- jQuery :eq()
- jQuery :even
- jQuery :file
- jQuery :first-child
- jQuery :first-of-type
- jQuery :first
- jQuery :focus
- jQuery :gt()
- jQuery Has Attribute
- jQuery :has()
- jQuery :header
- jQuery :hidden
- jQuery #id
- jQuery :image
- jQuery :input
- jQuery :lang()
- jQuery :last-child
- jQuery :last-of-type
- jQuery :last
- jQuery :lt()
- jQuery [name=”value”][name2=”value2″]
- jQuery (“selector1, selector2, selectorN”)
- jQuery (“prev + next”)
- jQuery (“prev ~ siblings”)
- jQuery :not()
- jQuery :nth-child()
- jQuery :nth-last-child()
- jQuery :nth-last-of-type()
- jQuery :nth-of-type()
- jQuery :odd
- jQuery :only-child
- jQuery :only-of-type
- jQuery :parent
- jQuery :password
- jQuery :radio
- jQuery :reset
- jQuery :root
- jQuery :selected
- jQuery :submit
- jQuery :target
- jQuery :text
- jQuery :visible
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:
$(":root")
📝 Example
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.jsCopied$(":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.
Setting Document-Level Variables:
You can use the
:root
selector to set and access CSS variables at the document level. For example:index.htmlCopied:root { --main-color: #3498db; }
example.jsCopied$(":root").css("--main-color", "#ff0000");
This will change the value of the --main-color variable to red (hex code #ff0000) throughout the document.
Applying Document-Wide Event Handlers:
You can bind event handlers to the root element to handle events globally across the document. For instance:
example.jsCopied$(":root").on("click", function() { alert("Document clicked!"); });
This will trigger an alert whenever any part of the document is clicked.
Accessing Document-Level Data Attributes:
The
:root
selector can be useful for accessing data attributes defined at the document level. For example:index.htmlCopied<html data-theme="light">
example.jsCopiedvar 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:
Author
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
If you have any doubts regarding this article (jQuery :root Selector), please comment here. I will help you immediately.