When I try to check the width of some text by calling the .width()
of the span
the text is in, I always get a wrong value at first. Then why I call .width()
again, I get the right value.
I'm adding the text using .append()
then I want to get the width of it immediately. When I open my file through Chrome directly (not using the Internet to get to Dropbox.com), it gets the width value correctly 99% of the time. Could this be a loading issue?
Here's an example: https://dl.dropboxusercontent.com/u/98788053/School/test.html
<script>
$(document).ready(function() {
$("body").append("<span id=\"element\">Text to measure</span>");
$("body").append("<div id=\"thewidth\"></div>");
$("body").append("<input type=\"button\" value=\"Try again\" onclick=\"getWidth();\">");
getWidth();
});
function getWidth()
{
$("#thewidth").html($("#element").width()+"px");
}
</script>