Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter HTML Collection in JavaScript?

Tags:

javascript

Hi I have problem with filtering HTML collection. I obtained list of classes as html collection. One of those class have .active class. I need to remove all other classes from this list and left only next one AFTER active class. Please how to do that?

Example of my list:

HTMLCollection []
0: div.chapter-list-item.seen
1: div.chapter-list-item.seen
2: div.chapter-list-item.seen
3: div.chapter-list-item.seen
4: div.chapter-list-item.active.seen
5: div.chapter-list-item.seen
6: div.chapter-list-item.seen
7: div.chapter-list-item.seen
8: div.chapter-list-item.

My code:

let allChaptersItems= document.getElementsByClassName("chapter-list-item");
let activeChapter = document.getElementsByClassName("active");
console.log(activeChapter);
console.log(allChaptersItems);
like image 419
Young L. Avatar asked Oct 21 '20 06:10

Young L.


People also ask

What is the difference between collect() and filter() method in JavaScript?

The filter () method is used to filter the given collection using the given callback function and returns the collection element. Parameters: The collect () method takes one argument that is converted into the collection and then filter () method is applied on it.

What is the importance of filtering in JavaScript?

Needless to say, the importance of filtering using JavaScript cannot be overstated. In JavaScript, the filter () method allows us to filter through an array - iterating over the existing values, and returning only the ones that fit certain criteria, into a new array.

How to filter through an array in JavaScript?

In JavaScript, the filter () method allows us to filter through an array - iterating over the existing values, and returning only the ones that fit certain criteria, into a new array. The filter () function runs a conditional expression against each entry in an array. If this conditional evaluates to true, the element is added to the output array.

What is an htmlcollection object?

An HTMLCollection object is an array-like list of HTML elements. Methods like the getElementsByTagName () returns an HTMLCollection.


1 Answers

You can directly query to get the items you want using the :not() selector to prevent matching items you don't want:

const chapters = document.querySelectorAll(".chapter-list-item:not(.active)");

console.log("Found elements:")
for (const chapter of chapters) {
  console.log(chapter.textContent, chapter.className)
}
<div class="chapter-list-item seen">One</div>
<div class="chapter-list-item seen other">Two</div>
<div class="chapter-list-item seen active">Three</div>
<div class="chapter-list-item seen">Four</div>

However, if you already have some elements and want to filter them, you can convert to array and them use Array#filter to check if the "active" class is not in the list of classes:

const existingElements = document.querySelectorAll(".chapter-list-item");

const chapters = Array.from(existingElements)
  .filter(chapter => !chapter.classList.contains("active"))

console.log("Found elements:")
for (const chapter of chapters) {
  console.log(chapter.textContent, chapter.className)
}
<div class="chapter-list-item seen">One</div>
<div class="chapter-list-item seen other">Two</div>
<div class="chapter-list-item seen active">Three</div>
<div class="chapter-list-item seen">Four</div>
like image 107
VLAZ Avatar answered Oct 06 '22 00:10

VLAZ