If the image has no width set in CSS, it will equal 0. That happens
because when ready() fires the image hasn't been loaded yet, so the
browser has no idea of it's size. In this case you have to use the
onload event.

- ricardo

On Nov 15, 11:59 am, Fluffy Convict <[EMAIL PROTECTED]>
wrote:
> I'm rewriting my own js library to jquery functions. A huge
> undertaking, but hopefully worth it in the future :) Question: why
> does the script always return 0 for img_width? Logo.gif exists and
> moving the code to the end of the page (before the closing body-tag)
> doesn't help either. Any help would be greatly appreciated!
>
>   <html>
>   <head>
>     <script type="text/javascript" src="jquery-1.2.6.js"></script>
>     <script>
>     $(document).ready(function(){
>       $("img.caption").each(function(i) {
>         var img_width = $(this).width();
>         var img_title = $(this).attr('title');
>         var img_align = $(this).attr('align');
>         $(this).wrap("<div class=\"image-caption-container\" style=
> \"float:" + img_align + "\"></div>");
>         $(this).parent().width(img_width);
>         $(this).parent().append("<div class=\"image-caption\">" +
> img_title + "</div>");
>       });
>     });
>     </script>
>   </head>
>   <body>
>     <img src="logo.gif" class="caption" title="scripting like a
> rockstar">
>   </body>
>   </html>

Reply via email to