6

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/

hoylemd
  • 436
  • 5
  • 15
  • *"...based on it's unique class combination."* What does that mean? It seems different from the example you gave, which isn't about a unique class combination, but (seemingly) just about an element having both of those classes... – T.J. Crowder Mar 14 '13 at 15:35
  • I mean that for my example, every element has a unique combination of classes. in my jsfiddle, for instance, only one div has both the "jon" and "stark" classes. – hoylemd Mar 14 '13 at 15:39
  • Looks like it is. I didn't think to search for "multiple" though. I'll try to think of more synonyms next time, and maybe my tiny bit of reputation won't get wiped out again. – hoylemd Mar 14 '13 at 15:58

2 Answers2

11

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?

iappwebdev
  • 5,880
  • 1
  • 30
  • 47
4

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.

Brad M
  • 7,857
  • 1
  • 23
  • 40
  • 1
    How was this down-voted? It's a valid answer, and I even specify that it's terrible, only because the obvious solution is just so obvious. – Brad M Mar 14 '13 at 15:38
  • 1
    A valid answer would also be: `var $elem = $('.class1'); $elem = ($elem.hasClass('class2')) ? $elem : $();`. But nobody would do that. And you have to admit that everyone is trying to find the best solution... and yours isn't. Why would you use that? – iappwebdev Mar 14 '13 at 15:41
  • 1
    @Simon Trying to find the best solution?? A simple search would reveal TONS of posts on how to select two classes with jQuery, this question is worthless and thus deserves a worthless answer. – Brad M Mar 14 '13 at 15:43
  • 1
    Well I dont't think that that guy knew about what he actually wanted to look for, as you can see in his question title. But if you want to give a worthless answer then don't be surprised when you get downvoted. Your answer might be right, but quality gets voted as well. – iappwebdev Mar 14 '13 at 15:47
  • 1
    Ironic since in the in the main thread for this topic, this identical answer got 42 votes. http://stackoverflow.com/questions/1041344/jquery-multiple-class-selector – Brad M Mar 14 '13 at 15:48
  • I'll remove my downvote I'm not sure where this dicussion will go. In my opinion, it should be an appeal to find the best/fastest solution and not just any solution. But if you have other thoughts about that... keep going. – iappwebdev Mar 14 '13 at 15:58
  • @BradM for what it's worth, I had an extremely convoluted situation where, for reasons too complicated to type out, the better solution was not applicable. Yours, on the other hand, worked, and saved me a lot of headache. – Lee A. Apr 06 '18 at 21:54