views:

62

answers:

3

What I want:


So I have a textarea.

I write Hello World into the the textarea.

I want to get the Hello World's Height in Javascript.

What I have tried yet:


    element = document.getElementById('textarea');

    var textHeight = element.innerHTML.offsetHeight;

    or this:
    var textHeight = element.value.offsetHeight;

But these are not working.

+2  A: 

Create a span element, set Span's innerHTML to "Hello World".
Get the span's offsetHeight.

var span = document.createElement("span");
span.innerHTML="Hello World"; //or whatever string you want.
span.offsetHeight // this is the answer

note that you must set the span's font style to the textarea's font style.

Your example will NEVER work because innerHTML and value are both strings. String doesn't define offsetWidth.

If you wish to get the height of selected text inside of a textarea, use selectionStart/selectionEnd to find the selected text of the textarea.

ItzWarty
aham so I should create a `<span>` inside the `<textarea>`, right?
CIRK
No. The span does not need to be appended to the document at all. It will be disposed of after the above code. I should also note that the code gives the height of the text WITH top/bot padding, as always with a span/div/whatever. If you don't want this padding, CSS can be used.
ItzWarty
Aham :D I think I've got it. Let me try it.
CIRK
-1 is because of?
ItzWarty
I don't know :|, It wasn't me.
CIRK
Did it work for you?
ItzWarty
I'm working on it.
CIRK
I should also note that your question was ambiguous, so i renamed it to the current title. It's probably not what you want. Do you want to get the height of "hello world" inside of the textarea? or what?
ItzWarty
I want to get the height of the text what I write in the textarea, your answer is a tricky way to do this, but in my script I have some other things too, currently I'm working on it.
CIRK
I think, if you tell us what you want to make with this height, it would be more clear to us.I put +1 to ItsWarty since it's a common solution he proposed.
Kaaviar
CIRK, i have replied to your other question about an auto-resizable textarea with a mockup code. It's not perfect, but hopefully it'll help you, or any other developer wishing to give it a shot.
ItzWarty
A `Range` is not going to help you. Firstly, `Range` does not apply to the value of text inputs: there's the `selectionStart` and `selectionEnd` properties of the textarea to cover that. Secondly, `Range` is all about nodes and offsets within the DOM and not about any kind of visual representation. Thirdly, `Range` doesn't exist in IE (until version 9, which does have it).
Tim Down
I was thinking of those two =/ correcting.
ItzWarty
Thanks very much for your help! works perfectly ;)
CIRK
A: 

http://www.quirksmode.org/dom/range_intro.html sorry that I can't be of more help.

the problem with you example is that inline text does not have a height, it only has a line-height, for it to have a height it needs to be in display block mode, so that all the lines are added to a block of text, even then it all depends on the width of the box and the font-size, font-family etc.

what ItzWarty suggests is getting the text selection and putting it in a div that has the same font and width as the textarea, witch has display block and allows you to get its height.

YuriKolovsky
A: 

element.scrollHeight is probably worth investigating.

If I was going to approach this (and I've not tested this at all), I'd set the textarea's height to 1px measure the scroll height and then reset the textarea's height.

https://developer.mozilla.org/en/DOM/element.scrollHeight

edeverett