Apu
December 02, 2022 ›
#class
›
#dynamically
❌
Add class to all children except first child
In this article, I will talk about how to add a class name to all child elements except the first child.
So, for this you can use the :not() selector with the combination of :first-child pseudo class.
<style>
.element{color:red}
</style>
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<script>
const liList = document.querySelectorAll('ol li:not(:first-child)');
liList.forEach((e)=>{
e.classList.add('element')
})
</script>
- In the above example, I have selected all li elements except the first using the querySelectorAll() method and stored in a variable liList as a NodeList.
- Then using the forEach() method, I add the class name element to all li elements.
save
listen
AI Answer
Add class to all children except first child
0
In this article, I will talk about how to add a class name to all child elements except t…
asked
Apu
0 answers
2915
In this article, I will talk about how to add a class name to all child elements except t…
Answer Link
answered
Apu
