Add Class To A Parent Element Using JavaScript | Bobbyhadz
Maybe your like
# Add class to a parent Element using JavaScript
To add a class to a parent element:
- Select the child element.
- Use the parentElement property to get access to the parent node.
- Call the classList.add() method on the parent, passing it the class name as a parameter.
Here is the HTML for the examples.
index.htmlCopied!<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> <style> .yellow-bg { background-color: yellow; } </style> </head> <body> <div data-id="parent-1"> <div id="child">Child 1</div> </div> <script src="index.js"></script> </body> </html> The code for this article is available on GitHubAnd here is the related JavaScript code.
index.jsCopied!const child = document.getElementById('child'); child.parentElement.classList.add('yellow-bg'); The code for this article is available on GitHub
We used the parentElement property on the node to get access to its parent.
The next step is to use the classList.add() method to add a class to the parent.
You can pass as many classes to the add() method as necessary.
index.jsCopied!const child = document.getElementById('child'); child.parentElement.classList.add( 'yellow-bg', 'second-class', 'third-class' ); If a class is already present on the node, the add() method will omit the specific class.Similarly, if you need to remove one or more classes from the parent, use the remove() method.
index.jsCopied!child.parentElement.classList.add( 'yellow-bg', 'second-class', 'third-class' ); child.parentElement.classList.remove( 'yellow-bg', 'second-class' );# Add a class to a parent element using closest
If you need to add a class to an element that isn't a direct parent of the node, use the closest() method to select the element.
The closest() method traverses the Element and its parents until it finds a node that matches the provided selector.Let's say that we need to select the div with id of parent-1 in this example.
index.htmlCopied!<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> <style> .yellow-bg { background-color: yellow; } </style> </head> <body> <div id="parent-1" data-id="parent-1"> <span id="parent-2"> <div id="child">Child 1</div> </span> </div> <script src="index.js"></script> </body> </html> The code for this article is available on GitHubHere is how we would add a class to the div element using JavaScript.
index.jsCopied!const child = document.getElementById('child'); child.closest('#parent-1').classList.add('yellow-bg');
If the element itself matches the selector, the element is returned.
If no element that matches the provided selector exists, the closest() method returns null.
The method takes a string that contains a selector. The provided selector can be as specific as necessary.
index.jsCopied!const child = document.getElementById('child'); child.closest('body > div#parent-1').classList.add('yellow-bg');We used the closest() method to select a div element with an id of parent-1, which has the body element as its parent.
If an invalid selector string is provided to the closest() method, a SyntaxError is thrown.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
- Add a class to the Body element using JavaScript
- Add a class to the Clicked Element using JavaScript
- Add a class to multiple Elements using JavaScript
- Add event listener to all Elements with Class in JavaScript
Tag » Add Class To Parent Element React Js
-
Add/Remove Class To Parent DOM Element React Js - Stack Overflow
-
Add Class To Parent Element - JavaScript Questions
-
React Change Attribute Of Parent Element Code Example
-
How To Add ClassName To Children Component In React Code Example
-
JavaScript Add Class To A Parent Element - AskAvy
-
JQuery Parent() Method - W3Schools
-
HTML DOM ParentElement Property - W3Schools
-
Les Refs Et Le DOM - React
-
How To Add Or Remove A Class To A Parent Element In Vue.js ... - Quora
-
Composition Ou Héritage ? – React
-
Les Portails - React
-
How To Apply Style To Parent If It Has Child With CSS? - GeeksforGeeks
-
How To Get The Closest Parent Element By Tag In JavaScript
-
JAVASCRIPT Add Class To Parent Div - JSFiddle - Code Playground