views:

52

answers:

3

Hello All,

This code works fine in FF, Chrome & Safari, but IE 8 is throwing several Invalid Argument Errors when I run it:

<script type="text/javascript">

    $(window).load(function() {
        $('div .box').each(function() {
            $(this).width($(this).find('img').width());
        });
    });

</script> 

You can see it in context here: My html/css skills are more or less spotless but I'm practically clueless with jquery/javascript. I have searched but cant get a handle on what the error is and how to fix it. Any help greatly appreciated.

Thanks.

A: 

is there any other information on the exception, like which line it is? I'm getting the feeling that the width of the image is not being found so you are setting the width of this with null. try and substitute the img width with something like 100 and see if it still throws.

Darko Z
Hi Darki Z, thanks for the input.line 116 char 165 of jquery js apparently.To substitute the value, do I do like this? ` $(window).load(function() { $('div .box').each(function() { $('100').width($(this).find('img').width()); }); });`If that is correct, then you are also correct. Is there a solution?
theothersimon
A: 

Looking at the markup on your page, you should have

$(window).load(function() {  
    $('div.box').each(function() {  
        $(this).width($(this).find('img').width());  
    });  
});

Notice the missing space between div and .box. If you write them separately, that means you are looking for all the elements with class box which are children of div while without the space it finds all the div with a class box

abhaga
Thanks. Good point, though I think Josh below has cracked it.
theothersimon
+1  A: 

The code "$(this).find('img')" is not finding an image in all cases. Try:

$(window).load(function() {
  $('div .box').each(function() {
    var imageJQObject = $(this).find('img');
    if(imageJQObject.length > 0) {
      $(this).width(imageJQObject.width());
     }
  });
});
JoshNaro
+1, hit it on the head - ie does not like 'nullpx'
redsquare
Josh, I think you cracked it. Thanks man. 3 invalid arguments, 3 boxes without imgs. that makes sense. Your edited codes works perfectly. Is there anything i need to know about it?
theothersimon
Not really "if(imageJQObject.length > 0)" just checks to see if there are objects in imageJQObject before operating on it. The "imageJQObject.width()" code will return null if imageJQObject is empty and setting the width to null is no good.
JoshNaro