Harry
Harry

Reputation: 89

How to remove class of element has not img tag using jquery?

I want to remove the class description when no <img> tag is found in all the divs with .box1:

<ul class="radio accomodation-radio">
   <li class="homeing-outer0 homeing-outer clearfix active" style="display: block;">
       <div class="box1 clearfix">
            <div class="description">
                 <p>Preis pro Person, nur Mehrbettzimmerbelegung. Sie teilen sich Hotelzimmer im Großraum Reykjavik und 2-4-Bett-Zimmer mit bezogenen Betten während der Tour.</p>
            </div>
       </div>
</li>....

I tried this, but that is not working:

$(".accomodation-radio").find(".box1 img").each( function(){      
    if ($(this).length === 0){ 
        $(this).find("div").removeClass(".description"); 
    }
});

Upvotes: 4

Views: 445

Answers (3)

Ehsan
Ehsan

Reputation: 12959

Try this :

$(document).ready(function(){

    $(".accomodation-radio .box1").each(function(){

        if($(this).find("img").length === 0)

            $(this).find(".description").removeClass("description");

    })

})

Upvotes: 0

Jameson the dog
Jameson the dog

Reputation: 1806

Your .each() is trying to run on elemnts that don't exist - you need to pass the .find inside the loop like this:

$(".accomodation-radio").each(function() {
    if ($(this).find(".box1 img").length === 0)
    { 
        $(this).find("div").removeClass("description"); 
    }
});

Upvotes: 3

Satpal
Satpal

Reputation: 133403

You can use combination of :not() and :has() selector

$(".accomodation-radio .box1:not(:has(img)) div").removeClass("description")

$(".accomodation-radio .box1:not(:has(img)) div").removeClass("description")
.box1 .description{color:green}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="radio accomodation-radio">
   <li class="homeing-outer0 homeing-outer clearfix active" style="display: block;">
       <div class="box1 clearfix">
            <div class="description">
                 <p>Preis pro Person, nur Mehrbettzimmerbelegung. Sie teilen sich Hotelzimmer im Großraum Reykjavik und 2-4-Bett-Zimmer mit bezogenen Betten während der Tour.</p>
            </div>
       </div>
</li>

Upvotes: 4

Related Questions