Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery find element by specific class when element has multiple classes

So I am working on something that wasn't well thought out in the build from the backend team. That leaves me with a document full of divs.

What I am doing is rolling back from the element I need to click on, get the parent container then find an element within the parent which has class="alert-box warn", class="alert-box dead", etc... Essentially, I'm trying to use multiple class selectors on each element. When I try to find just alert-box it doesn't seem to be working right. I'm assuming because it has warn,dead, ``fine, etc...

How can I find just alert-box* or equivalent to a wildcard concept?

like image 613
chris Avatar asked Sep 28 '11 18:09

chris


People also ask

How can I select an element with multiple classes in jQuery?

The . class selector can also be used to select multiple classes. Note: Seperate each class with a comma. Note: Do not start a class attribute with a number.

How do you find an element with multiple classes?

Use the getElementsByClassName method to get elements by multiple class names, e.g. document. getElementsByClassName('box green') . The method returns an array-like object containing all the elements that have all of the given class names.

How do you target a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

How to find certain class within the element using jQuery?

All the above-specified specified script helps you to find certain class within the element. The hasClass () method is able to search single or multiple classes on the selector element. You only need to specify your classes name in the method separated by space (" "). If you found this tutorial helpful then don't forget to share.

How to check if a class exists or not in jQuery?

Method 1: Using hasClass() method: The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. It returns a boolean value specifying whether the class exists in the element or not. This can be used to check for multiple classes.

How do I select elements with multiple classes names?

If you want to target the elements with multiple classes names, such as selecting the elements only if it has both classA and classB, or has classes classA, classB,...,classX or something like that, just put the class selectors together without any space in between, as shown below:

How to check if a class is available in an element?

Using attr () method to get the classes of an element and make an Array with it by split the value by space (" ") if there is more than one class. With indexOf () method search a class name within the Array if it returns value greater than 0 then the class is available in the element. 5. Demo Click on the Check class button.


2 Answers

You can combine selectors like this

$(".alert-box.warn, .alert-box.dead");

Or if you want a wildcard use the attribute-contains selector

$("[class*='alert-box']");

Note: Preferably you would know the element type or tag when using the selectors above. Knowing the tag can make the selector more efficient.

$("div.alert-box.warn, div.alert-box.dead");
$("div[class*='alert-box']");
like image 153
John Hartsock Avatar answered Oct 09 '22 00:10

John Hartsock


You can select elements with multiple classes like so:

$("element.firstClass.anotherClass");

Simply chain the next class onto the first one, without a space (spaces mean "children of").

like image 11
Bojangles Avatar answered Oct 08 '22 23:10

Bojangles