ysanmiguel
ysanmiguel

Reputation: 455

Delete Attr and add it if class exist - jquery

Hello I have a problem with this small code:

$(".delayDiv").each(function(){
    var img_attr = $(this).attr('style');
    $(this).attr('style', '');
    if ($(".delayDiv").hasClass("visible")) {
        $(this).attr('style', img_attr);
    }
});

What I'm trying to do is:

DIV outsite the viewport: <div class="backImg"></div>

DIV inside the viewport: <div class="backImg visible"></div>

Thank you for any help.

Upvotes: 1

Views: 542

Answers (2)

Rahul Patel
Rahul Patel

Reputation: 5246

Remove the style only if element do not have 'visible' class.

$(document).ready(function(){
    $(".delayDiv").each(function(){
        if (!$(".delayDiv").hasClass("visible")) {
            $(this).removeAttr('style', '');
        }
    }); 
})

Upvotes: 1

charlietfl
charlietfl

Reputation: 171689

All you need is a :not() selector and removeAttr() to remove the style from all the main class that doesn't also have the visible class

$(".delayDiv:not(.visible)").removeAttr('style')

Upvotes: 1

Related Questions