With some help of the people here, I've been able to get the original width of an image with the following code:
var img = new Image();
img.onload = function () {
var W2 = this.width;
alert(W2);
}
img.src = document.getElementById('imageViewerImg').src;
Now that I have the width of an image, I need a conditional statement to modify the CSS if an image is wider than 700px.
My code to do this looks like this:
$(document).ready(function(){
var img = new Image();
img.onload = function () {
var W2 = this.width;
}
img.src = document.getElementById('imageViewerImg').src;
if($W2 > 700) {
$("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
}
});
But this won't work. I've tried to add an alert box in the conditional statement, so if an image happens to be wider than 700px, it should appear and notify. This alert box won't show up either. Even if I put the alert box outside the if statement and just put in below, it won't work, like this:
$(document).ready(function(){
var img = new Image();
img.onload = function () {
var W2 = this.width;
}
img.src = document.getElementById('imageViewerImg').src;
alert(W2)
});
Where should I put my if statement in this code to make it work?
divsto show getting width and using it in a conditional.