Reputation: 455
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:
class="delayDiv"
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
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
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