Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Select an element only when it has no class name

How can I use jQuery to select an element only if it doesn't have any classes?

I'm writing a page which allows the html author to override the default jQuery action by adding a class to the element. It should be any class at all.

So the markup might be:

<ul>
  <li class="override"></li>
  <li></li>
</ul>

I'd like jQuery to only select the second list element because it doesn't have a class.

I've looked at .hasClass(), but it seems to require a specific class name.

Thanks!

like image 960
Christopher Werby Avatar asked Jan 28 '12 01:01

Christopher Werby


People also ask

How do you select an element without a class?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

How do I select an element in jQuery without an ID or class?

var noSpace= $('*:not([id])*:not([class])'); // 84 ?

Has not class jQuery selector?

Given a list of elements and the task is to not select a particular class using JQuery. jQuery :not() Selector: This selector selects all elements except the specified element. Parameters: It contains single parameter selector which is required.

How to select specific element in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.


1 Answers

:not() Selector – jQuery API

$('li:not([class])')
like image 156
Ali Demirci Avatar answered Sep 19 '22 12:09

Ali Demirci