textarea/.net textbox scroll to end

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Webster

    textarea/.net textbox scroll to end

    Hi,
    is there a way using JavaScript to tell a textarea to scroll to the end of
    it's contents? I'm implementing something that looks like messenger using
    web forms, after each post the chat area gets updated but I have to manually
    scroll it down to the end.

    Rob


  • Martin Honnen

    #2
    Re: textarea/.net textbox scroll to end



    Rob Webster wrote:

    [color=blue]
    > is there a way using JavaScript to tell a textarea to scroll to the end of
    > it's contents?[/color]

    With some browsers (tested successfully with IE 6, Netscape 7.1, Mozilla
    1.7, should work with IE 5/5.5, Netscape 7.0, Mozilla 1.x too) the
    following works

    <html lang="en">
    <head>
    <title>scrollin g a textarea to its end</title>
    <script type="text/javascript">
    function scrollElementTo End (element) {
    if (typeof element.scrollT op != 'undefined' &&
    typeof element.scrollH eight != 'undefined') {
    element.scrollT op = element.scrollH eight;
    }
    }

    function randomFill (textarea) {
    var numberOfLines = Math.floor(Math .random() * 20) + 10;
    var text = '';
    for (var i = 1; i <= numberOfLines; i++) {
    text += 'Line ' + i + '\r\n';
    }
    textarea.value = text;
    }
    </script>
    </head>
    <body>
    <form action="" onsubmit="retur n false;" name="formName" >
    <p>
    <textarea name="textareaN ame" rows="10" cols="80"></textarea>
    </p>
    <p>
    <input type="button" value="random fill, scroll to end"
    onclick="random Fill(this.form. textareaName);
    scrollElementTo End(this.form.t extareaName);">
    </p>
    </form>
    </body>
    </html>

    Opera 7.50 doesn't scroll the textarea however although it seems to
    support scrollTop and scrollHeight, I am not sure what the problem is
    without looking into it further.
    Netscape 4 doesn't support that.

    --

    Martin Honnen


    Comment

    Working...