You need to wait for the image to load:
function setModalPicture(picName){
//Build the path to the picture
var pic= 'assets/img/art/'+picName;
//Set the picture
var img = $('#g-modal-img');
img.one("load", adjustModalPadding).attr('src', pic);
if (img[0].complete) {
img.off("load", adjustModalPadding);
adjustModalPadding();
}
}
Note the sequence above, because it's important:
- First, hook the
load
event with a one-off handler (one
).
- Then, set the
src
.
- Check if the image is already complete: If so, remove the handler and call your function immediately; otherwise, when
load
fires, it will call adjustModalPadding
and remove it as a handler.
You may want to add error handling to that...
Here's a working example:
function setModalPicture(picName) {
//Build the path to the picture
var pic = picName; // 'assets/img/art/'+picName;
//Set the picture
var img = $('#g-modal-img');
img.one("load", adjustModalPadding).attr('src', pic);
console.log("img[0].complete after setting src: " + img[0].complete);
if (img[0].complete) {
img.off("load", adjustModalPadding);
adjustModalPadding();
}
}
function adjustModalPadding() {
var img = $("#g-modal-img")[0];
console.log("Size: " + img.width + "x" + img.height);
}
$("input[type=button]").on("click", function() {
console.log("img[0].complete before starting: " + $("#g-modal-img")[0].complete);
setModalPicture("https://graph.facebook.com/1035045703246692/picture?type=large");
});
<!-- In a comment, you said it starts out with src="" -->
<img id="g-modal-img" src="">
<input type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
That works for me in Chrome, Firefox, and IE11.
Alternately, you might create a replacement img
element by cloning:
function setModalPicture(picName) {
//Build the path to the picture
var pic = picName; // 'assets/img/art/'+picName;
//Set the picture
var img = $("#g-modal-img");
var newImage = img.clone();
img.replaceWith(newImage);
newImage.one("load", adjustModalPadding).attr('src', pic);
console.log("newImage[0].complete after setting src: " + newImage[0].complete);
if (newImage[0].complete) {
newImage.off("load", adjustModalPadding);
adjustModalPadding();
}
}
function adjustModalPadding() {
var img = $("#g-modal-img")[0];
console.log("Size: " + img.width + "x" + img.height);
}
$("input[type=button]").on("click", function() {
console.log("img[0].complete before starting: " + $("#g-modal-img")[0].complete);
setModalPicture("https://graph.facebook.com/1035045703246692/picture?type=large");
});
<!-- In a comment, you said it starts out with src="" -->
<img id="g-modal-img" src="">
<input type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
That also works for me in Chrome, Firefox, and IE11.
Finally, you might create a replacement img
element from scratch (not cloning):
function setModalPicture(picName) {
//Build the path to the picture
var pic = picName; // 'assets/img/art/'+picName;
//Set the picture
var img = $("#g-modal-img");
var newImage = $("<img>").attr("id", "g-modal-img");
img.replaceWith(newImage);
newImage.one("load", adjustModalPadding).attr('src', pic);
console.log("newImage[0].complete after setting src: " + newImage[0].complete);
if (newImage[0].complete) {
newImage.off("load", adjustModalPadding);
adjustModalPadding();
}
}
function adjustModalPadding() {
var img = $("#g-modal-img")[0];
console.log("Size: " + img.width + "x" + img.height);
}
$("input[type=button]").on("click", function() {
console.log("img[0].complete before starting: " + $("#g-modal-img")[0].complete);
setModalPicture("https://graph.facebook.com/1035045703246692/picture?type=large");
});
<!-- In a comment, you said it starts out with src="" -->
<img id="g-modal-img" src="">
<input type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>