Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  139] [ 1]  / answers: 1 / hits: 29135  / 10 Years ago, fri, october 24, 2014, 12:00:00

I have a textarea that I append data to with textarea.value += more textn; and I would like it to stay scrolled to the bottom, so it would always show the last line.



I have read that I should do:



var textarea = document.getElementById('textarea_id');
textarea.scrollTop = textarea.scrollHeight;


But I tried that (http://jsfiddle.net/BenjiWiebe/mya0u1zo/) and I can't get it to work.



What am I doing wrong?


More From » javascript

 Answers
26

You need to set scrollTop each time you append text:



var textarea = document.getElementById('textarea_id');
setInterval(function(){
textarea.value += Math.random()+'n';
textarea.scrollTop = textarea.scrollHeight;
}, 1000);


http://jsfiddle.net/mya0u1zo/2/


[#69021] Wednesday, October 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
demondp

Total Points: 154
Total Questions: 97
Total Answers: 99

Location: Mali
Member since Thu, Jul 9, 2020
4 Years ago
;