I'm working for a client who's using a third-party platform which only allows me to override CSS and partially edit the HTML templates. I can also make changes by adding Javascript, and in this instance I need to append classes and IDs based on the dimensions of thumbnail images, represented as images within list-items. I've started a fiddle, replacing the images with a div.
http://jsfiddle.net/dbudell/SYTsP/4/
Here's the HTML:
<ul>
<li><div class="item item1"></div></li>
<li><div class="item item2"></div></li>
</ul>
And the CSS. As you can see, I've set .item1 to "portrait" dimensions and .item2 to "landscape dimensions. I want to append to IDs based on these dimensions:
li {
display:inline-block;
margin:5px;
}
.item1, .item2 {
border:1px solid #333;
}
.item1 {
width:100px;
height:200px;
}
.item2 {
width:200px;
height:100px;
}
#fill-portrait {
background:#333;
}
#fill-landscape {
background:#888;
}
And here's the JQuery code, which seems to be syntactically correct but is yielding no results. Not sure what the problem is.
var itemWidth = $('.item').width();
var itemHeight = $('.item').height();
if (itemWidth > itemheight) {
$('.item', this).addAttr('id','fill-landscape');
} else {
$('.item', this).addAttr('id','fill-portrait');
}
Again, the link to the fiddle is http://jsfiddle.net/dbudell/SYTsP/4/.
You need to select the individual elements you're trying to get the dimensions of. Otherwise, you're $('li') selector is selecting every "li" element on the page. For example:
Then you can add an id as follows:
An explicit example http://jsfiddle.net/SYTsP/7/:
Now with more iteration!!
http://jsfiddle.net/SYTsP/9/