Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery .width() returns 0 zero before appending the image to the browser
    primarykey
    data
    text
    <p>I'm using jquery and i'm trying to create a slideshow.</p> <p><strong>Before i append the image</strong>, i shall know <strong>the Width of the image</strong>. So i use <em>.width()</em> of jquery and <em>.attr("width").</em></p> <p>My <strong>code</strong> is as follows:</p> <pre><code>var attr_href = $next.attr('href'); /*returns the href of the image*/ var $item = $('&lt;img src="'+attr_href+'" /&gt;'); //$item contains the image, the image is not displayed to the browser yet!!! var itemWidth = $item.width(); alert(itemWidth); // ---&gt; returns zero 0 in Mozilla and in IE var itemWidth2 = $item.attr("width"); alert(itemWidth2); //In Mozilla, the first time it returns 0.After the image is loaded, it returns the right Width //In IE, always returns 0 zero !!! //Now i have to append the photo and i have to use the right Width of the image!!! var final_img = $('&lt;img src="'+attr_href+'" width="'+itemWidth+'"/&gt;'); $('.SlideShow').append(final_img); </code></pre> <p>So, i tried to use the <strong>.load()</strong> and i did:</p> <pre><code>var attr_href = $next.attr('href'); /*returns the href of the image*/ var $item = $('&lt;img src="'+attr_href+'" /&gt;'); //$item contains the image, the image is not displayed to the browser yet!!! $item.load(function () { var WidthAfterLoading = $item.attr("width"); var WidthAfterLoading2 = $item.width(); }); alert(WidthAfterLoading); //returns "undefined" in both IE and Mozilla alert(WidthAfterLoading2); //returns "undefined" in both IE and Mozilla </code></pre> <p>So, before i append the image to the browser, i shall know the right width of the image. But i get zero(0) and undefined.</p> <p>Is there any other way, in order to take the <strong>right width of the image??</strong></p> <p>Thanks, in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload