How to remove an element class doesn't have an img tag using jquery?

I want to remove the class description when the tag is <img>not found in all 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 this does not work:

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

You can use combination :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>
Run codeHide result
+4
source

Yours is .each()trying to work on elements that do not exist - you need to pass .findinside the loop like this:

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

Try the following:

$(document).ready(function(){

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

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

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

    })

})
0
source

Source: https://habr.com/ru/post/1656213/


All Articles