How to get the height in textarea in javascript

Asked 2 years ago, Updated 2 years ago, 99 views

Textarea enables scrolling, so if the number of characters is large, the height of the textarea itself and the height of the contents will change.

Is there a way to get the height of this content?

javascript jquery jquery-ui

2022-09-30 20:46

2 Answers

$('textarea').scrollTop()+$('textarea').innerHeight()

I guess so...


2022-09-30 20:46

You can retrieve it at Element.scrollHeight.

var area= document.getElementById('area');
var contentHeight=area.scrollHeight;

alert('The content height is'+contentHeight+'px.');

if(confirm('Do you want to adjust the element height?)')) {
  area.style.height=contentHeight+'px';
}
<textareaid="area" style="width:250px;height:100px;overflow-y:scroll;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nisi sem, sodales ac dolor quis, elementum varius massa. Sed eget odio tincidunt, dignissim justo eget, tempus est. Cras imperdiet tempus metus a mattis. Nullam pretium ultricies sapien sit amet iaculis. Integer eget dignissim odio, non ullamcorper augue. Pellentesque a interdum sapien. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per acceptos himenaeos. Intortorrisus, elementum non conque sed, brandit neccu. Nam laoreet tincidunt effectur.
</textarea>


2022-09-30 20:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.