Dorvalla
Dorvalla

Reputation: 5240

Searching for item in each list item

I am working on a piece of code to check if there is in the list item a div called star rating. If it doesnt excist, add to the price tag an extra margin top (so i can level out these items). My JQuery is as followed

$(document).ready(function() {
    $("li.product").each(function(){
        if($(".star-rating").length === 0) {            
            $("span.price").css({"margin-top" : "1em"});
        }
    });
});

This code works fine to a certain point. What it technically does is it doesnt check the specific list item but the whole document. Is there a way I can just check the specific list item and add to the span.pricein that item the css? cause now it finds 4 times the star-rating (4 list items), while it is only in one list item.

Upvotes: 0

Views: 78

Answers (3)

waycell
waycell

Reputation: 31

$('li.product').not(':has(.star-rating)').find("span.price").css({"margin-top" : "1em"});

Without "if" and "each" that's what Jquery is for...

Upvotes: 0

Igor
Igor

Reputation: 15893

Search under each "li.product":

$("li.product").each(function(){
    if($(this).find(".star-rating").length === 0) {            
        $(this).find("span.price").css({"margin-top" : "1em"});
    }
});

Upvotes: 2

Akshey Bhat
Akshey Bhat

Reputation: 8545

if($(".star-rating",this).length === 0) {            
            $("span.price",this).css({"margin-top" : "1em"});
        }

Add context parameter to jQuery method

Upvotes: 2

Related Questions