Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select an element only if it doesn't have a class assigned?

Tags:

jquery

css

I'm modifying the CSS of an existing WordPress theme. The theme has a lot of special styles for lists, attached to the <li> element. Because of this, there is a generic list-style:none rule applied to the <li> element.

I want to update the CSS to reinstitute the list-style defaults on <li> elements that don't have a class attached (i.e. those that don't have a special style applied). This needs to be selective to keep from ruining the fancy effects.

I am aware of the :not() pseudo-selector, and may end up using it to specifically exclude each possible class; however, there are a dozen of them and this seems like a maintenance nightmare.

Is there any way, either using CSS or jQuery, to select an element by specifying that it has no class set?

like image 409
Sarah Lewis Avatar asked May 09 '11 19:05

Sarah Lewis


People also ask

How do you select an element without 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 you select an element with a specific class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.

How do you select all elements but one in CSS?

In this article, we will learn how to create a CSS rule for all elements except one specific class. Approach: Use the :not(selector), also known as negation pseudo-class which takes a simple selector as an argument and allows you to style all the elements except the element specified by the selector.

How do I choose a div class?

Use document.querySelector on to select a div and then select an element within it with the given class after that. We just call querySelector on the element with the ID mydiv to select items inside that div. Therefore, innerDiv is the div with the class myclass .


2 Answers

$('li:not([class])'); should do what you need.

Considering Dan's comment - since you didn't specify I assumed you meant the li would have no class attribute at all. If you need to grab those that are <li class> or <li class="">...

$('li:not([class])');
$('li[class=]');
$('li[class=""]');
like image 144
David Fells Avatar answered Nov 15 '22 05:11

David Fells


jQuery: $('li[class=]') (selects both elements that have no class attribute as well as those for which the attribute is present, but empty)

The CSS selector li[class=""] unfortunately does not work the same way. It selects only those elements for which the attribute is present, but empty (ex: <li class></li> or <li class=""></li>)

The $('li:not([class])') solution might not be what you want (it doesn't select elements that have the class attribute present but empty).

Given:

<ul>
  <li>item 1</li>
  <li class>item 2</li>
  <li class="">item 3</li>
  <li class="some-class">item 4</li>
</ul>

$('li[class=]') selects 1,2,3
$('li[class=""]') selects 2,3
$('li:not([class])') selects just 1

like image 28
Dan Manastireanu Avatar answered Nov 15 '22 05:11

Dan Manastireanu