How to automate the size of the text area in height?

I set the width of the text box to 100%, but now I need to know how many characters can fit on one line.

I am trying to write a javascript function to automatically create / compress a text field. I try not to use jquery since I just need this function.

My logic is rows = textarea.value.split('\n'), iterating through rowsand count += rows[i].length/textarea.colsthen count += rows.lengthand finally textarea.rows = count. The only problem is that it is counttoo large because it is textarea.colstoo small.

+3
source share
1 answer

(textarea, ) . , .

function autoHeight(element){
    element.style.height='auto';
    element.style.height=element.scrollHeight+'px';
}

, , , .

+2

Source: https://habr.com/ru/post/1782478/


All Articles