Correct nodelist is not generating

I am trying to get only those article tags, which don’t have either holidayclass or holidayclass2. Is the logical && not working here?

Try using a filter. The following should work:

const articles = document.querySelectorAll("article.commonclass");
var noHoliday = Array.from(articles).filter(function noHoliday(article) {
    const isHoliday = article.classList.contains("holidayclass")
        || article.classList.contains("holidayclass2");
    return !isHoliday;
});
console.log(noHoliday);
1 Like

On testing the not selector further, the following seems to works just fine.

const noHoliday = document.querySelectorAll(
    "article.commonclass:not(.holidayclass,.holidayclass2)"
);
console.log(noHoliday);
2 Likes

Both are excellent methods. Thanks.

In CSS we have nth-child, is there a way to get last-child of this node?
Number of items may be variable in the nodelist, this example was not exhaustive.

I tried many things →

By hit & trial this worked to get last element in the nodelist →

var noHoliday = document.querySelectorAll("article.commonclass:not(.holidayclass,.holidayclass2)");
console.log(noHoliday);
var nSibling = noHoliday[noHoliday.length-1].nextElementSibling;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.