String Pixel Length

Discussion in 'JavaScript' started by Pudge1, Jan 20, 2014.

  1. #1
    I want to make a textbox stretch to the size of the text inside it (the text will change to different lengths) plus roughly 75px. So what I was planning on doing was making a hidden <span></span> with the text inside of it and then getting the clientWidth of that, adding the 75px to it and then applying it to that textbox. Is this possible?

    Also I would prefer to do it in em, if it isn't possible in em can you convert it using pixels and the client's screen resolution?

    Thanks in advance!
     
    Pudge1, Jan 20, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    generally speaking because you CANNOT rely on the font you choose being loaded or even existing, there's no real reliable way to do what you are asking. Generally speaking it's the type of trickery that has no business on a page or in a design...

    THOUGH, to what end are you making it change size based on it's contents... it sounds like something that really has no business being done to a textarea in the first place.... when you say "text box" you do mean <textarea>, right?

    If not, why not use a SPAN and set it to "inline-block"? Pad one size 75px, done. No scripting or other goofyness needed.

    "textbox" could mean almost anything... At first I thought you meant textarea, but that could mean a DIV, or a H1, or some other tag...
     
    deathshadow, Jan 20, 2014 IP