I am working on a page where the selected item among a list is characterized by NOT having a given class. Something like the following:
-
In such case you may consider attribute selector like this:
console.log(document.querySelectorAll('li[class="a"]').length)
li[class="a"] {
color:red;
}
- Select me
- Don't select me
- Don't select me
- Select me
Simply pay attention to extra spaces:
console.log(document.querySelectorAll('li[class="a"]').length)
li[class="a"] {
color:red;
}
- Pay attention to this one !!
- Don't select me
- Select me
But since you are using JS you can use trim() to get rid of the extra spaces:
var elem=document.querySelectorAll('li');
for(var i=0;i
li[class="a"] {
color:red;
}
- Pay attention to this one !!
- Don't select me
- Select me