Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to get the intersect of selectors in jQuery [duplicate]

Tags:

jquery

I have a set of 4 elements. each one belongs to 2 of 4 classes, so each class combination is unique. I want to find a way, with jQuery, to select only one of the elements, based on it's unique class combination. Is this possible?

I'm expecting something like

$(".class1 & .class2")

is this reasonable?

jsFiddle example: http://jsfiddle.net/7UfYH/

like image 392
hoylemd Avatar asked Mar 14 '13 15:03

hoylemd


2 Answers

You can do

$(".class1.class2")

But I actally don't get what your fiddle has to do do with that. What are you trying to achieve?

like image 55
iappwebdev Avatar answered Nov 07 '22 04:11

iappwebdev


For a terrible solution that still works...

$('.class1').filter('.class2');

or

$('.class2').filter('.class1');

Note, the $('.class1.class2') selector is unquestionably the fastest solution. But if any people who see this answer who never knew what filter() was, now they have a decent grasp, and may be able to expand on that logic.

Consider if someone asks "How do I select an element with ID whatever in jQuery?" Obviously, the fastest/most obvious answer is $('#id'). But answers such as $('*[id="' + id + '"]') provide alternative insight which may or may not be more useful than the obvious answer itself.

like image 25
Brad M Avatar answered Nov 07 '22 03:11

Brad M