jQuery width() returns 0 for a span element created on the fly?

Posted by Lyon on Stack Overflow See other posts from Stack Overflow or by Lyon
Published on 2010-03-26T17:05:37Z Indexed on 2010/03/26 17:13 UTC
Read the original article Hit count: 389

Filed under:
|

Hi,

I'm trying to determine the width of a string. I'm using the following code but it always return 0. It seems to be that as the span element is created and not already present in the page body, jQuery width() returns 0. Is there a way such that I can get the width of the text without resorting to creating dummy html code in the page?

Thanks :)

$('<span>test</span>').width(); // width = 0
$('span.width').width(); // returns a width value

<body><span class="width">test</span></body>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about width