1

can I find 2 classes in same html-tag/html-container without using a foreach ?

I was thinking of something like this, to find out whether the same child has 2 classes:

$(".btnUploadWrapper").children().hasClass("postFileSuccess").hasClass("hidden")

or

$(".btnUploadWrapper").children().hasClass("postFileSuccess hidden")

Is it possible ?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
kkkk00999
  • 179
  • 2
  • 13
  • Yes, that's good, but how about the other way round, if one child should only contain a single class ?! – kkkk00999 Jul 22 '16 at 08:58

2 Answers2

1

You can use find on the parent to get all the elements with the two classes inside them. You can directly use the elementy any further:

var elements = $(".btnUploadWrapper").find(".postFileSuccess.hidden");

If you only want to get the direct children, you can do the same with children too:

var elements = $(".btnUploadWrapper").children(".postFileSuccess.hidden");
eisbehr
  • 12,243
  • 7
  • 38
  • 63
  • Don't forget that `find()` goes all the way down the DOM tree from the first element, whereas the OPs original usage of `children()` only gets child elements – Rory McCrossan Jul 22 '16 at 09:00
0

If you don't want to use a loop directly, you could try and select the elements and check the length of the resulting jQuery object:

var $matched = $(".btnUploadWrapper > .postFileSuccess.hidden");
if ($matched.length) {
    // there are some matching elements...
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339