views:

196

answers:

7

I'm trying to scale images that have a width greater than 100. I'm using the code below, but it scales images that are even below 100px... What am I doing wrong?

if($(".image-attach-body")) {
    if($(".image-attach-body a")) {
    $(".image-attach-body a").each(function() {
                var width = $("span span img").width();

                if(width > 100) {
                    $("span span img").cjObjectScaler({
                            destObj: $(".image-attach-body"),
                                method: "fit",
                });
                }
    }); 
    }
}
A: 

Try wrapping the whole thing in $(document).load( );, the image might not be there yet.

$(document).load( function () {
 if($(".image-attach-body")) {
        $(".image-attach-body a").each(function() {
         var img = $("span span img",this);

         if(img.width() > 100) {
           img.cjObjectScaler({
             destObj: $(".image-attach-body"),
             method: "fit"
           });
         }
       });

 }

});

Hogan
No, it is... In fact, if I add in alert(width), it shows me the width of each of the images... And, it's within the $(document).ready(function () { }
phpN00b
documents are ready before all images are loaded. The time it takes to render the alert could change things...
Hogan
Try changing from ready(...) to load(...) and if that does not work then post the html and we will see what is going on.
Hogan
When you say "it shows me the width of each of the images" do you mean in several alert boxes or 1 alert box which says "153,100,290,444" for say, 4 images?
Erik
I mean, for example, I have 2 images, and I get two alert boxes, one with 240, the other with 16
phpN00b
Sorry, I had a brain fart and edited your answer instead of my own. Whoops, I rolled it back now.
R. Bemrose
@Bemrose, lol no problem.
Hogan
@Bemrose, 1 point from edit ability... arrrgh.
Hogan
+3  A: 

If all of your images were under 100, your code would work. Here is the problematic line of code...

$("span span img").cjObjectScaler

This selector is inside of your each loop. So if just one image is greater than 100, you invoke this function to all of them. The function call applies to every element that matches the selector (that's just how jQuery works).

I don't know what your markup looks like, so I can't tell you what to change the code to. Inside your each loop, you will likely need to utilize this somewhere in your selector so that it is more specific and relevant to the given context.

I would guess it needs to be changed to this...

$("span span img", this).cjObjectScaler

EDIT: You'll also need to change the line of code to do this where you get the width of the image, because that will always only return the width of the first image it finds. I'd recommend storing it in a local variable so you don't have to re-query for it later when you apply the "scaler"

Josh Stodola
Yes, but it's only looking for span span img within the image-attach-body a... right?
phpN00b
No. I will update answer to show you how to do that.
Josh Stodola
Nope, you will have to do $(this).find("span span img") or simply $("span span img", this) to have a limited scope
Vincent Robert
+2  A: 

The main problem, is you are not scoping the search, so your final $('span span img') is finding all the img in the page. Here is a function that fixes a few other problems as well. Please ask if it doesn't make sense:

if( $(".image-attach-body").length ) {
    $(".image-attach-body a").each(function() {
        var $img = $("span span img", this),
            width = $img.width();

        if(width > 100) {
            $img.cjObjectScaler({
                destObj: $img.closest(".image-attach-body"),
                method: "fit",
            });
        }
    }); 
}

Note: Your first if statement would have always returned true because it returns an empty jQuery object if nothing is found, not null like you might expect. So changing it to .length verifies if found at least 1. The second if statement (which I removed) was unnecessary because the each loop would have run 0 times if no objects matched, so the test was wasted... and had the same problem as the first, in that it would always run.

Doug Neiner
I tried this, but I get an $img.closest is not a function error?
phpN00b
Ok, I took at $img.closest, and it worked! Thanks so much
phpN00b
A: 

i think your width() function is always evaluating your first image. Try this instead:

if($(".image-attach-body")) {
    if($(".image-attach-body a")) {
    $(".image-attach-body a").each(function() {
    var width = $("span span img",$(this)).width();
pixeline
Tried this, but I get the same result...
phpN00b
A: 

There are several problems involving your selectors, as well as with the width command only returning the width of the first element in a selector.

In fact, I suggest you change the code to something like this:

$(".image-attach-body a").each(function() {
    var images = $("span span img").filter(function() {
        return this.width > 100;
    });

    images.cjObjectScaler({
        destObj: $(".image-attach-body"),
        method: "fit"
    });
});
R. Bemrose
A: 

Seeing that I don't know where cjObjectScaler function came from I did this change (based on your code...)

if($(".image-attach-body")) {
    if($(".image-attach-body a")) {
    $(".image-attach-body a").each(function() {
                var $img = $("span span img", this);
                var width = $img.width();

                if(width > 100) {
                    img.attr("width", "100"); //Obviously, you'll replace it with your cjObjectScaler function here....
                });
                }
    }); 
    }
}
The Elite Gentleman
A: 

$("span span img") selects images in the whole page, not relative to the current scope.

Additionnaly, you don't have to test for a jQuery before using each. This method simply does nothing on an empty jQuery so your code can be simplified to:

// For each attach body in document
$(".image-attach-body").each(function(attachBody)
{
    // For each image in the attach body
    $("a span span img", attachBody).each(function() 
    {
        // If image is too large
        if( $(this).width() > 100 )
        {
            // Scale to attach body
            $(this).cjObjectScaler({
                destObj: $(attachBody),
                method: "fit",
            });
        }
    });
});
Vincent Robert