Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to down in textarea with jQuery

<h2>Greetings</h2>
<div class="container">
  <div class="inner">
    Hello
    <p>Test</p>
  </div>
  <textarea id="one" class="inner">
    Goodbye
</textarea>
</div>

$("#one").append("your text to append");
$("#one").append("your text to append");
$("#one").append("your text to append");
$("#one").append("your text to append");

LIVE: http://jsfiddle.net/tGFmq/

how can i make automatically scroll to down in this textarea?

like image 630
Roy Genergon Avatar asked Dec 11 '11 20:12

Roy Genergon


3 Answers

Add this bit to your code (preferably at the end of whatever inserts you have):

    var psconsole = $('#one');
    if(psconsole.length)
       psconsole.scrollTop(psconsole[0].scrollHeight - psconsole.height());
like image 128
Romanulus Avatar answered Oct 15 '22 17:10

Romanulus


See this Live Demo: here

To calculate the bottom scrollTop, you can simply subtract the height from the scrollHeight:

var oneDiv = $("#one");
bottom = oneDiv.prop('scrollHeight') - oneDiv.height()

Then you can set its scrollTop to bottom, or use amazing jQuery's animate() for cool animation.

Live Demo: here

like image 29
Derek 朕會功夫 Avatar answered Oct 15 '22 17:10

Derek 朕會功夫


I realised my problem was that I had the code in the incorrect place. -> Placed under the element and got the problem to solve (rookie mistake....) -- Just a reminder to all.

like image 4
Mitchell Reynolds Avatar answered Oct 15 '22 17:10

Mitchell Reynolds