Dynamically Scrolling a Textarea
Asked Answered
B

5

14

I have a textarea html element on my page that gets reloaded via ajax. The whole textarea is returned each time not just its content, and the content grows over time. Along with the textarea i return the following piece of javascript:

<script type="text/javascript" >

var textArea = document.getElementById('outputTextResultsArea');
textArea.scrollTop = textArea.scrollHeight;
</script>

In firefox 3.0.7 this places the scroll bar at the bottom of the textArea, allowing me to see the latest of the output. However in IE 7 i see different behaviour. The scrollbar moves down with the content as intended, but once the content is greater then the textarea height the scroll bar no longer moves down. It seems as if IE is remembering the original scroll height of the element, not the new height.

I am using the xhtml transitional doctype if that helps. Also if this can be achieved with jQuery that would be fine as I have access to that.

Thanks in advance

Neil

Busywork answered 13/3, 2009 at 12:4 Comment(0)
G
21

As a quick hack you can just do this:

textArea.scrollTop = 99999;

Another option is to try it in a timer:

setTimeout(function()
{
    var textArea = document.getElementById('outputTextResultsArea');
    textArea.scrollTop = textArea.scrollHeight;
}, 10);
Grote answered 13/3, 2009 at 12:9 Comment(1)
thanks this does indeed work! I'll leave the question open just incase there any other suggestions.Busywork
M
4

Using jQuery, $("textarea").scrollHeight(99999) works great on Firefox and Chrome but not on IE. It appears to set it to the max number of lines in the textarea, whereas scrollHeight is supposed to be the number of pixels. (Awesome show great job IE). This appears to work though:

      $("textarea").scrollTop(99999)
      $("textarea").scrollTop($("textarea").scrollTop()*12)

I think this assumes a 12px font-height. I would love to find a more robust/straightforward way to do this.

Magnetron answered 8/11, 2010 at 3:46 Comment(0)
B
3

Instead of using the timeout, call that function on every AJAX response - provided you can tweak it.

That would free your browser from unnecessary timeouts.

Batista answered 13/3, 2009 at 12:22 Comment(1)
I think he is already doing that, otherwise it wouldn't be working in FirefoxGrote
I
2

I ended up using this for Internet Explorer:

textArea.createTextRange().scrollIntoView(false);

and this for other browsers:

textArea.scrollTop = textArea.scrollHeight;
Iseult answered 30/3, 2012 at 15:19 Comment(0)
R
0

For using like textarea in your example give your textarea a name like "id=LiveTextArea"

Then add buttom of your javascript this (LiveTextArea is the id name):

LiveTextArea.scrollTop = LiveTextArea.scrollHeight;

So would look like:

var textArea = document.getElementById('outputTextResultsArea');

textArea.scrollTop = textArea.scrollHeight;

Now the textarea will dynamicly scroll caret/down on any new entry.

Radman answered 13/6, 2020 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.