Rob Webster wrote:
is there a way using JavaScript to tell a textarea to scroll to the end of
it's contents?
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>scrolling a textarea to its end</title>
<script type="text/javascript">
function scrollElementToEnd (element) {
if (typeof element.scrollTop != 'undefined' &&
typeof element.scrollHeight != 'undefined') {
element.scrollTop = element.scrollHeight;
}
}
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="return false;" name="formName">
<p>
<textarea name="textareaName" rows="10" cols="80"></textarea>
</p>
<p>
<input type="button" value="random fill, scroll to end"
onclick="randomFill(this.form.textareaName);
scrollElementToEnd(this.form.textareaName);">
</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
http://JavaScript.FAQTs.com/