1

I have label, which has classes status-label and unavailable. I want to count all those containing it and exclude those, which have also class named "hide"

<label class="status-label unavailable"> should be selected
while
<label class="status-label unavailable hide"> shouldn't

I have tried to count the result like this:

var invalidConfigs=$("label.status-label.unavailable").not(".hide").length;

this didn't work either:

var invalidConfigs=$("label.status-label.unavailable:not(.hide)).length;
7
  • 1
    The first version of your code works fine - jsfiddle.net/gn601536 The second version also works without the missing " - jsfiddle.net/gn601536 Commented Nov 10, 2015 at 12:06
  • $("label.status-label.unavailable").filter(":not('.hide')").length Commented Nov 10, 2015 at 12:08
  • than I must have something wrong elsewhere... Commented Nov 10, 2015 at 12:08
  • Try using the browser console - wickedlysmart.com/hfjsconsole Commented Nov 10, 2015 at 12:11
  • 1
    Um, there is no problem using the browser console even with iframes like in the stackoverflow editor. Commented Nov 10, 2015 at 12:20

2 Answers 2

1

var invalidConfigs = $("label.status-label.unavailable:not(.hide)")
   .css("color", "red") // just for demo purposes
   .length;
console.log(invalidConfigs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="status-label unavailable"> should be selected</label>
<label class="status-label unavailable hide"> shouldn't</label>

You are missing an end quote in your selector.

.css("color", "red") is just for the demo to make it visually apparent which element is selected.

Sign up to request clarification or add additional context in comments.

4 Comments

The difference here is that jQuery will use the :not pseudo selector if the browser supports it while .not('.hide') will filter the result set after the fact. The former being slightly faster if you have a huge amount of elements.
Can you add an explanation how this is any different from the code that was in the question?
Well for starters it does not have a syntax error... The error in the OP's code was so small that I don't feel a lengthy explanation is need.
No "lengthy" explanation, but a simple "you missed a quote" might have been the problem (however unlikely). Without any explanation, it looks like you just copied the question, fixed the typo then changed how it worked to add a colour instead of returning the count (despite the question asking for the count) - ie it looks like this adds nothing. Do you see why I asked?
1

How about this one:

var hideClassLabels=$('label.hide');
var notHideLabels=$('label.status-label').not(hideClassLabels);
console.log(notHideLabels.length);

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.