Get string length in pixels with JavaScript
Asked Answered
B

3

4

Let's say I have the string "Hello". This string is obviously five characters in length, but what is its length in pixels? Is there an easy way to determine this length in JavaScript? I have thought of a solution where an extra div would have to be displayed to the user, but this way seems hacky and complicated.

In the bigger picture, I am trying to determine how many spaces would be necessary to fill that length of the string. As you can probably tell from above, I think the best option would be to simply measure the length of the string and a single space character from the user's perspective and calculate how many spaces should replace the text based off of that. This is going to be displayed in an HTML input text, by the way.

Thanks.

Brusa answered 5/2, 2011 at 23:27 Comment(3)
You probably want the width in pixels of an element, which content is the string "Hello". Am I right?Horwitz
I suggest using a monospaced font then you would know how many spaces it takes. ;) en.wikipedia.org/wiki/Monospaced_fontMohammadmohammed
@JoeRobich I believe unicode throws off the 1-character-1-glyph assumption :-)Azov
S
2

You can determine the number of pixels the container of the string. You can do this by creating a hidden element in the DOM, setting the inner HTML to the string and then asking for the width.

Selfconfidence answered 5/2, 2011 at 23:34 Comment(3)
How do you ask for the width? I tried it lbl.style.width and lbl.width but its always a undefined valueCircumstantiate
@NetMage this answer doesn't require jQuery, and it does explain how to calculate the string width.Advertise
@Advertise No idea why I wrote that... :)Plum
M
2

I don't think their is a good, easy way to do that, except computing the length of a container. The width depends on the font-size, the font, the letter-spacing, it will be different depending on the browser etc...

Mcclellan answered 5/2, 2011 at 23:31 Comment(0)
S
2

You can determine the number of pixels the container of the string. You can do this by creating a hidden element in the DOM, setting the inner HTML to the string and then asking for the width.

Selfconfidence answered 5/2, 2011 at 23:34 Comment(3)
How do you ask for the width? I tried it lbl.style.width and lbl.width but its always a undefined valueCircumstantiate
@NetMage this answer doesn't require jQuery, and it does explain how to calculate the string width.Advertise
@Advertise No idea why I wrote that... :)Plum
E
0

I've used this to determine the text length in pixels (jQuery syntax):

var txtLen = $(<selector>).text().length * $(<selector>).css('font-size').slice(0,-2);

where selector is whatever you use to target the specific element. Adjust where needed (if the font-size is not set in px).

For example:

var txtLen = $('table td').text().length * $('table td').css('font-size').slice(0,-2);

Real-life use of this was when I needed to determine whether to show the tooltip on a td with a fixed width - if td is wider than its contents, there is no need to show the tooltip.

Things stated in Robin's answer still apply.

Eclat answered 25/1, 2021 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.