Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery CSS 'or' selector

I am trying to select elements of a (certain class || another class) with the same selector. How can I go about doing that?

Currently, I have:

$(".class1 .class2").each(function(idx, el) {... }); 

however, that only selects elements that match both classes, not one or the other.

How can I select elements that match one or both of the classes, with the same selector?

like image 907
Erin Drummond Avatar asked Aug 10 '10 02:08

Erin Drummond


People also ask

Are jQuery selectors the same as CSS selectors?

Yes, they are practically equal, jQuery could have some extra selectors documented in http://api.jquery.com/category/selectors but most important: you can use for e.g. $('#foo img') or $('. a, . b, #c') and in your css you can use :checked{...}

Is jQuery a selector?

The is( selector ) method checks the current selection against an expression and returns true, if at least one element of the selection fits the given selector. If no element fits, or the selector is not valid, then the response will be 'false'.

Which selector is faster in jQuery?

ID and Element selector are the fastest selectors in jQuery.

What is the use of jQuery selectors?

jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.


2 Answers

Try this

$(".class1,.class2") 

http://api.jquery.com/multiple-selector/

like image 173
Nikita Rybak Avatar answered Sep 24 '22 02:09

Nikita Rybak


$(".class1,.class2").each(function(idx, el) {... }); 

put a comma within the same selector string.

http://api.jquery.com/multiple-selector/

like image 29
lukemh Avatar answered Sep 26 '22 02:09

lukemh