Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery matching multiple classes

Tags:

jquery

For the first time, I got in this kind of unusual situtation

I have to select a div on which more than 2 classes are applied, that to in random order

For Example:

Assume that I have a <div> on which following 3 classes are applied content-text, right-align and bold-font. I need to select this div, but problem is that classes are applied in different order on different pages

<div class="content-text right-align bold-font">...</div>
<div class="right-align content-text bold-font">...</div>
<div class="content-text bold-font right-align ">...</div>
...

How can I select this particular div, no matter how classes are applied?

Thanks

like image 960
I-M-JM Avatar asked Jan 19 '11 05:01

I-M-JM


People also ask

How do you find the element of 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. Here is the HTML for the examples in this article.

How do you target multiple elements in jQuery?

Use the * selector to select all elements in a document.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

How can use two find in jQuery?

Use a comma to union multiple queries: var tds = $(this). find('input, textarea');


1 Answers

This will work fine. CSS selectors in general don't care about the order of classes, just the ones that are applied. This also goes for jQuery:

$('div.content-text.right-align.bold-font');
like image 109
coreyward Avatar answered Sep 18 '22 13:09

coreyward