Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Select Element That Does Not have Specific Class

People also ask

How do you select non classes in CSS?

The :not() pseudo-class requires a comma-separated list of one or more selectors as its argument. The list must not contain another negation selector or a pseudo-element.

How do you know if an element does not have a class?

Use the classList. contains() method to check if an element does not have a specific class, e.g. if (! el. classList.

Which selector will finds elements with a specific class?

The class selector selects HTML elements with a specific class attribute.


This selects the second LI element.

document.querySelector("li:not([class])")

or

document.querySelector("li:not(.completed):not(.selected)")

Example:

// select li which doesn't have a 'class' attribute...
console.log(document.querySelector("li:not([class])"))

// select li which doesn't have a '.completed' and a '.selected' class...
console.log(document.querySelector("li:not(.completed):not(.selected)"))
 <ul id="tasks">
    <li class="completed selected">One Task</li>
    <li>Two Task</li>
  </ul>

To select the <li> that has not completed nor selected class:

document.querySelector("li:not(.completed):not(.selected)");

Fiddle

http://jsfiddle.net/Z8djF/


You can try the :not() selector

var completeTask = document.querySelector("li:not(.completed):not(.selected)");

http://jsfiddle.net/UM3j5/


document.querySelectorAll('[wf-body=details] input:not(.switch):not(.btn)').forEach(function(e){
    // do whatever you want. with 'e' as element :P
});

Try getting an array of the parent's children instead:

var completeTask = document.querySelector("#tasks").childNodes;

Then loop/search them as necessary.


The :not(*selector*) selector also accepts commas (so does querySelectorAll()):

let plainElements = document.querySelectorAll( ':not( .completed, .in-progress ) ');
plainElements.forEach( ( item ) => { item.style.color = 'red'; } );
li { color: green; }
<ul id="tasks">
  <li class="completed selected">Task 1</li>
  <li>Task 2</li>
  <li class="in-progress">Task 3</li>
</ul>