Hello, let's dive into the code.
<div class="nyhet"> <img src="#" width="785" class="attachement-full" /></div>
<div class="nyhet"> <img src="#" width="390" class="attachement-full" /></div>
<div class="nyhet"> <img src="#" width="785" class="attachement-full" /></div>
<div class="nyhet"> <img src="#" width="390" class="attachement-full" /></div>
<div class="nyhet"> <img src="#" width="785" class="attachement-full" /></div>
<div class="nyhet"> <img src="#" width="390" class="attachement-full" /></div>
<div class="nyhet"> <img src="#" width="785" class="attachement-full" /></div>
<div class="nyhet"> <img src="#" width="390" class="attachement-full" /></div>
Alright, so here's what I'm aiming for - adding a class of "large" to divs with images that have a width of 785 and a class of "small" to divs with images that have a width of 390.
This is the desired outcome:
<div class="nyhet large"> <img src="#" width="785" class="attachement-full" /></div>
<div class="nyhet small"> <img src="#" width="390" class="attachement-full" /></div>
<div class="nyhet large"> <img src="#" width="785" class="attachement-full" /></div>
and so forth. This is my attempt so far.
$(document).ready(function(){
var x = $(".nyhet").length;
for (var i=0; i<x;i++){
n = $(".attachement-full:eq(i)").attr("width");
if (n<785) {
$(".nyhet:eq(i)").addClass("small");
} else {
$(".nyhet:eq(i)").addClass("large");
}
}
});
I opted for using .attr instead of .width due to potential image loading delays when the script runs.
Your assistance on this matter would be greatly appreciated!