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?
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.
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.
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.
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.
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.
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:
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.
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']");
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").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With