3

I'm trying to get the original width of an image with JQuery and make some adjustments in the CSS with a condition if an image is wider than 700px.

I used this code to get the width of an image:

var img = new Image();
img.src = $('#imageViewerImg').attr('src');
img.onload = function () {
    var W2 = this.width;
    alert(''+ W2);
}

Fiddle

You can see that an alert box shows the width of the image, in this case 1024. When I copy paste this code, it doesn't work on my website. It simply won't display the alert box. I have JQuery included properly, as other JQuery codes work, it's just this simple piece of JQuery that doesn't do what it's supposed to do.

At the end, this is the code I need for the functionality I'm trying to create:

var img = new Image();
img.src = $('#imageViewerImg').attr('src');
img.onload = function() {
    var imgWidth = this.width;
}
if($imgWidth > 700) {
    $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
}

Why does the alert box show up on JSfiddle but not on my own PC?


Also with the document.ready function, it still won't work. The JQuery is simply not being executed.

3
  • 5
    Did you remember the document ready function, jsFiddle will add that for you automagically. Commented Jul 23, 2013 at 14:47
  • What browser are you using? Commented Jul 23, 2013 at 14:48
  • 1
    $imgWidth is undeclared in your code. and imgWidth is useless in this context. Commented Jul 23, 2013 at 15:01

4 Answers 4

2

Use the jQuery document ready function like this:

$(document).ready(function(){

});

This is a common "bug" with code that is copied from jsFiddles.

Sign up to request clarification or add additional context in comments.

Comments

1

Try it like this:

var img = new Image();
img.onload = function () {
    var W2 = this.width;
    if(W2 > 700) {
        var photoHolder = document.getElementById("photoHolder");
        photoHolder.style.verticalAlign = 'none';
        photoHolder.style.textAlign = 'none';
    }
}
img.src = document.getElementById('imageViewerImg').src;

Also, open the console and check for errors, check that you only have one element with that ID etc.

1 Comment

@user2611052 - note that this doesn't use jQuery and it sets the source after the load function.
0

Use that to be sure that your code will be executed after the html loading.

$(document).ready(function(){
    var img = new Image();

    img.src = $('#imageViewerImg').attr('src');

    img.onload = function() {
    var imgWidth = this.width;
    }

    if($imgWidth > 700) {
        $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
    }
});

Comments

0

your variable $imgWidth is undeclared in your code, and imgWidth will not have scope outside your function.

try:

var img = new Image();
img.src = $('#imageViewerImg').attr('src');
var imgWidth='';
img.onload = function() {
    imgWidth = this.width;
}
if(imgWidth > 700) {
    $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.