Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85565

remove images which don't have the class

I'm trying to remove the images which don't have the class.

html

<div id="slider">
<img class="" />
<img class="" />
<img class="img1" />
<img class="img2" />
<img class=" " />
</div>

jQuery

if(!$('#slider img').class()){
$('#slider img').remove(); // but I'm stucked at this line
}

Upvotes: 0

Views: 37

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388316

I think what you can do is remove images whose class attribute value does not begin with img

$('#slider img:not([class^=img])').remove();

but a more correct solution will be

$('#slider img').filter(function () {
    return $.trim(this.className).length === 0
}).remove();

Upvotes: 3

Andy Jones
Andy Jones

Reputation: 6275

Select everything, then filter out the things you don't want...

$("#slider img")
.filter(function(i) { return $(this).attr("class") == ""; })
.remove();

Upvotes: 0

ManZzup
ManZzup

Reputation: 526

$("#slider img").each(function(i){
         if($(this).class=""){
             //do whatever
         }
});

Upvotes: 0

Related Questions