Hi,

Probably you're just checking too early.  Try something like this:

* * * *
    var img;

    img = new Element('img');
    img.observe('load', function() {
        alert('The image is ' + this.getWidth() + ' wide');
    });
    img.src = 'someimage.png';
    $('container').appendChild(img);
* * * *

Note that I'm hooking the load event *before* setting the src
attribute.  This is important on IE -- sometimes it grabs the image so
fast, you hook the event after the image is loaded.

This may require some reworking of your logic, since you can't do
anything with the width until the image is loaded, which is
"later" (your function doing the above has completed).  (Of course, if
you're dealing with images you control, you can know the width in
advance and avoid all this, although it could become a maintenance
issue unless you have it automated into your build process...)

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Jul 3, 11:54 am, "[email protected]" <[email protected]> wrote:
> Hello,
>
> I'm trying to get the width of a created element by the following way:
>
> // create div element
> var itemDiv = new Element('div', {
>     'id' : 'item-1',
>     'class' : 'item'
>
> });
>
> // create image element
> var itemImage = new Element('img', {
>     'src' : itemPath + image['src'][0],
>     'alt' : ''
>
> });
>
> // append image to div
> itemDiv.appendChild(itemImage);
> var width = itemDiv.getWidth(); // always returns 0
>
> This code is executed in an each() function and every image got
> different dimensions. So the width of every div is different. I need
> the complete width of all created div elements. But the getWidth()
> function always returns 0. Even getDimensions() does not work. So is
> there any way to get the width of a created element?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to