5

Basically I need the width of a span. Do to the need to use several custom characters that are not found on a keyboard I am building my own "input" field, using a "div". Each character gets wrapped in a "span" tag that has an event listener attached. This will allow the user to click anywhere in the string and have the cursor move to a position after a character, it also allows them to add or delete characters in the middle of a string.

I am using "offsetLeft" and "offsetWidth" to find the right side of a character, the problem is when a character/span is clicked the "offsetWidth" is way off. So for example, if I am using a 14 pixel font an "M" will return as 35 pixels wide when it is actually 11 pixels. And there are several variations, an average sized character like an "S" will return at 26 pixels when it is actually 9. So there is variation in the sizing. Now you may wonder how I found the actual letter size and that was using Firebug. Which if Firebug can find it I would assume so can I, I just haven't figured out how. So I hope someone here knows.

I also tried using "getComputedStyle" and "currentStyle to find the width and it returns "auto". Also tried getBoundingClientRect().width it the same as offsetWidth, the difference being that it also finds the fractions of a pixel in width, so an "M" is 35.366668701171875 pixels wide, odd.

EDIT:

Based on user1289347 post I should have noted that the "offsetWidth" causes the "offsetLeft" to be off by the errant amount as well.

Krik
  • 445
  • 5
  • 15
  • 1
    Welcome to [SO]. I can see that you've posted a few times already, but I think you need to review [Markdown for SO](http://stackoverflow.com/editing-help). – zzzzBov Mar 26 '12 at 00:39
  • Have you used a javascript debugger to look at the style element in the dom and see what seems to be the best way to find this information? – James Black Mar 26 '12 at 00:39
  • firebug: https://getfirebug.com/layout – dsdsdsdsd Nov 04 '13 at 21:46

1 Answers1

-1

Try jquery width() it computes the dom width pretty well every time I've used it. http://api.jquery.com/width/

And if jquery is out of the question

Create a DIV styled with the following styles. In your JavaScript, set the font size and attributes that you are trying to measure, put your string in the DIV, then read the current width and height of the DIV. It will stretch to fit the contents and the size will be within a few pixels of the string rendered size.

HTML:

    <div id="Test">
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</div>

CSS:

    #Test
{
    position: absolute;
    visibility: hidden;
    height: auto;
    width: auto;
}

JavaScript (fragment):

var test = document.getElementById("Test");
test.style.fontSize = fontSize;
var height = (test.clientHeight + 1) + "px";
var width = (test.clientWidth + 1) + "px";

This will create a seperate testable div with your test, credit here Calculate text width with JavaScript It's a lot of work but it should get you better results by operating outside of your existing markup.

Community
  • 1
  • 1
user1289347
  • 2,397
  • 1
  • 13
  • 16