I answer my own question: it's better to use a span element.
function getElementWidth(Elem) {
if(document.getElementById) {
var elem = document.getElementById(Elem);
} else if (document.all){
var elem = document.all[Elem];
}
xPos = elem.offsetWidth;
return xPos;
}
<span id="test">asdfasdf</span>
alert(getElementWidth(test)) -> gets the span width
"Showtime" <di***************@usuarios.retecal.es> escribió en el mensaje
news:37*************@individual.net...
Hi! I have a text and I need to get its width in pixels. The font size is
not fixed. I've been suggested to put the text inside of an invisible div
and get the div's width. But I would need to force the div to shrink to
fit the text. Any idea?
Thanks in advance