Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"); 
    }
});
like image 803
Harry Avatar asked Mar 11 '23 00:03

Harry


2 Answers

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>
like image 55
Satpal Avatar answered Mar 13 '23 12:03

Satpal


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"); 
    }
});
like image 36
Jameson the dog Avatar answered Mar 13 '23 14:03

Jameson the dog