Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu November 19, 2022 . #Element . #HowTo

Select all child elements except the first

Today our problem is we want to select all child elements except the first one using JavaScript. For example, we have the following code.

<ol>
  <li>Home</li>
  <li>About</li>
  <li>Contact</li>
</ol>

Now, what we have to do is we have to select all li elements and add a class name child to both of them except the first li element. Like the following example.

Example : add class to li elements except the first. #
<ol>
  <li>Home</li>
  <li class="child">About</li>
  <li class="child">Contact</li>
</ol>

We can use the :not() and :first-child selectors. E.g.

document.querySelectorAll("li:not(:first-child)");
save
listen
AI Answer
Write Your Answer
loading
back
View All