How can I select an element with multiple classes in jQuery?
If you want to match only elements with both classes (an intersection, like a logical AND), just write the selectors together without spaces in between:
$('.a.b')
The order is not relevant, so you can also swap the classes:
$('.b.a')
So to match a div
element that has an ID of a
with classes b
and c
, you would write:
$('div#a.b.c')
(In practice, you most likely don't need to get that specific, and an ID or class selector by itself is usually enough: $('#a')
.)
getElementsByClassName() with two classes
You can't do it with getElementsByClassName()
method instead use querySelectorAll()
method with comma separated class selectors.
document.querySelectorAll('.a,.b')
How to get class-name of element with multiple classes but only knowing 1 of them
It would have been easier (and faster) to try to write some code and test your question directly instead of asking in SO.
Or maybe reading the documentation for getElementsByClassName
[Sigh] you don't hace to specify the exact multiple classes. Running:
document.getElementsByClassName("one")
will give you an array with the four divs.
Select element with two classes
You could use querySelector
(or querySelectorAll
if there's more than one matching element and you want to get a reference to all of them):
var elem = document.querySelector(".page.current");
You can also actually just use getElementsByClassName
, which accepts a space-separated list of class names:
var elems = document.getElementsByClassName("page current");
How to select an element with multiple classes in jQuery?
Question is very vague and doesn't explain exactly what you are trying to do but you probably want something like:
$(this).find("tr.well.answers-list.radio-list").filter(function(){
if($(this).hasClass('array2')){
st+="inside array";
}else{
st+="not array";
}
});
Using two CSS classes on one element
If you want two classes on one element, do it this way:
<div class="social first"></div>
Reference it in css like so:
.social.first {}
Example:
https://jsfiddle.net/tybro0103/covbtpaq/
How can I select an element with multiple classes in CSS?
No. You should combine individual class names using .
:
.twelve-col.col.main-menu
Find div element by multiple class names?
I don't think barak manos's answer has fully explained it.
Imagine we have few elements as the followings:
<div class="value test"></div>
<div class="value test "></div>
<div class="first value test last"></div>
<div class="test value"></div>
How XPath matches
Match only 1 (exact match), barak's answer
driver.findElement(By.xpath("//div[@class='value test']"));
Match 1, 2 and 3 (match class contains
value test
, class order matters)driver.findElement(By.xpath("//div[contains(@class, 'value test')]"));
Match 1, 2, 3 and 4 (as long as elements have class
value
andtest
)driver.findElement(By.xpath("//div[contains(@class, 'value') and contains(@class, 'test')]"));
Also, in cases like this, Css Selector is always in favor of XPath (fast, concise, native).
Match 1
driver.findElement(By.cssSelector("div[class='value test']"));
Match 1, 2 and 3
driver.findElement(By.cssSelector("div[class*='value test']"));
Match 1, 2, 3 and 4
driver.findElement(By.cssSelector("div.value.test"));
Related Topics
How Does the Math.Max.Apply() Work
How to Refresh an Iframe Using JavaScript
How to Prevent Your JavaScript Code from Being Stolen, Copied, and Viewed
Chrome Refuses to Execute an Ajax Script Due to Wrong Mime Type
Angular 5 Service to Read Local .JSON File
JavaScript - Populate Drop Down List with Array
Capturing JavaScript Console.Log
Mobile Safari Autofocus Text Field
Sort by Two Values Prioritizing on One of Them
Calling a JavaScript Function from Managed Bean
JavaScript Document.Write Replaces All Body Content When Using Ajax
JavaScript "Cannot Read Property "Bar" of Undefined
Addition Is Not Working in JavaScript
How to Build/Concatenate Strings in JavaScript
Differencebetween String.Slice and String.Substring
Differencebetween Substr and Substring