How to get the height of the text inside of a textarea

后端 未结 8 1611
眼角桃花
眼角桃花 2020-12-05 10:02

I have a textarea with the the text Hello World. I would like to get the height of this text.

I\'ve tried to use:

var eleme         


        
相关标签:
8条回答
  • 2020-12-05 10:31

    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-US/docs/Web/API/Element/scrollHeight

    0 讨论(0)
  • 2020-12-05 10:36

    You can get the text height by getting the textarea scrollbar height

    const textarea = document.getElementByTagName("textarea");
    const height = textarea.scrollHeight;
    
    console.log({ height });
    
    0 讨论(0)
  • 2020-12-05 10:38

    I am not sure whether I interpret your question correctly, but I personally needed to know the exact height of each line of text. The line-height property does not have the right value (for example, in Safari, it will be rounded to the closest value when actually printing text).

    This is my workaround. You should have the following code somewhere at the beginning of the document.

    // set one row in the textarea and get its height
    element.rows = 1;
    var height1 = parseFloat(window.getComputedStyle(element)["height"]);
    // set two rows in the textarea and get its height
    element.rows = 2;
    var height2 = parseFloat(window.getComputedStyle(element)["height"]);
    // Now, the line height should be the difference
    var inputLineHeight = height2-height1;
    

    The variable inputLineHeight should contain the correct value, in pixel.

    0 讨论(0)
  • 2020-12-05 10:40

    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.

    0 讨论(0)
  • 2020-12-05 10:42

    You can use element.scrollHeight (just like Patrick answered) but it needs some corrections (I'm using jQuery in example):

    1) if your textarea has padding, you need to substract it (top & bottom).

    2) if element has already set height, you need to set it to zero (just for measure then set it back, else it returns this height + padding)

    var h0 = $(element).height();  // backup height
    $(this).height(0); 
    var h = $(this).get(0).scrollHeight - $(this).css('paddingTop').replace('px','')*1 - $(this).css('paddingBottom').replace('px','')*1; // actual text height
    $(this).height(h0); // set back original height (or new height using h)
    

    There is no need of extra span with same css as textarea.

    0 讨论(0)
  • 2020-12-05 10:43

    For anyone using React:

    const textarea_ref = useRef(null);
    const [idealHeight,setIdealHeight] = useState(0);
    const [inputValue,setInputValue] = useState("");
    
    
    useLayoutEffect(() => {                                           // useLayoutEffect TO AVOID FLICKERING
        textarea_ref.current.style.height = '0px';                    // NEEDS TO SET HEIGHT TO ZERO
        const scrollHeight = textarea_ref.current.scrollHeight;       // TO READ THE CORRECT SCROLL HEIGHT WHEN IT SHRINKS
        textarea_ref.current.removeAttribute('style');                // REMOVE INLINE STYLE THAT WAS ADDED WITH 0px
        setIdealHeight(scrollHeight + 2);                             // NEEDS TO ADD 2. I THINK IT'S BECAUSE OF THE BORDER
      },[inputValue]);
    
    return (
      <textarea
        // USE idealHeight STATE TO SET THE HEIGHT
        value={inputValue}
        onChange={onChange}
        ref={textarea_ref}
      />
    );
    

    PS: It still flickers sometimes. At least in Chrome.

    0 讨论(0)
提交回复
热议问题