I have a textarea that I append data to with textarea.value += "more text\\n";
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?
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);
To answer the original question: since your string output ends with a newline, you should scroll before you output, not after (I keep my ids in an object called id):
function Output(Msg)
{
...
id.Log.scrollTop=id.Log.scrollHeight;
SetValue('Log',out);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.