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/
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?
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.
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