1

Not sure how to word this question... I have a script that does an each loop for every nth element.
I have this as a variable: var nthCard = $('.card:nth-child(' + col + 'n+' + i + ')'); then I do a .each with it nthCard.each(function(i){...}

After the each, I need to find out how many of nthCards have the class .featured.

The html is set up like so:

<div class="card"></div>
<div class="card featured"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card featured"></div>

Assuming this is the each statement, I need to find how many of these cards are also featured.

I tried something like this: console.log(nthCard.find('.featured').length); and also console.log(nthCard.hasClass('.featured').length); but the former returns 0 and the later returns undefined.

Here's the truncated code:

function placeCard(col){
    var i = 0;
    for(i=1; i<= col; i++){
        var nthCard = $('.card:nth-child(' + col + 'n+' + i + ')');
        nthCard.each(function(idx){
            //do some code
        });
        console.log(nthCard.find('.featured').length);//I need to return the number of featured cards here.
    }
}

3 Answers 3

4

filter the matched elements:

var number = nthCard.filter('.featured').length;
Sign up to request clarification or add additional context in comments.

1 Comment

Good ol' filter. I always forget about that one. Thank you, this was what I was looking for. I will accept in less than 10 minutes (when it allows me to.)
1

Try:

console.log(nthCard.hasClass('featured'));

2 Comments

hasClass returns true or false
Thanks, but I am trying to find the number of those 'featured' cards.
1

Why not count them while you are in the each loop?

function placeCard(col){
    var i = 0;
    for(i=1; i<= col; i++){
        var nthCard = $('.card:nth-child(' + col + 'n+' + i + ')');
        var count = 0;
        nthCard.each(function(idx, card){
            //do some code
            if ($(card).hasClass('featured')) {
                count += 1;
            }
        });
        console.log(count);//I need to return the number of featured cards here.
    }
}

Or, more optimally:

function placeCard(col){
    var i = 0;
    for(i=1; i<= col; i++){
        var nthCard = $('.card:nth-child(' + col + 'n+' + i + ')');
        var count = 0;
        nthCard.each(function(idx, card){
            //do some code
            if (card.className === 'card featured') {
                count += 1;
            }
        });
        console.log(count);//I need to return the number of featured cards here.
    }
}

4 Comments

I think @Blender's solution is cleaner and less resource intensive.
Another great example! Thank you! I tried this and changed $(card) to $(this) and it works the same as Blender's method, though Blender's method is cleaner.
@DeweyOx I suspect that as you are looping anyway, it's less resource intensive to use the loop. I've added another version that doesn't use jQuery in the loop, which will be more optimal.
@DeweyOx: You throw most concepts of efficiency out of the window when you use jQuery, so I don't think it makes any difference in this case.

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.