0

Cannot make a div element automatically scroll down.

$('#div').scrollTop(1000)

This doesn't work, but if set on click it works:

$('btn').click(function()
{
    $('#div').scrollTop(1000);
});

I need it to scroll down without clicking any button. What do I need to change?

Root149
  • 389
  • 1
  • 3
  • 11

3 Answers3

0
$(document).ready(function(){
    $('#div').scrollTop(1000);
  });
SierraOscar
  • 17,507
  • 6
  • 40
  • 68
0

Try running it after the document is ready:

$(function() { // short for $(document).ready(function() {
    $('#div').scrollTop(1000);
});

Chances are you're running your script before the element exists (check $('#div').length -- if it's 0, you're running it too soon). You need to wait until the DOM is loaded, which wrapping your code in the above will handle.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • It may actually be, since I'm using Ajax to add content to the div tag, thanks – Root149 Nov 24 '14 at 23:41
  • Yeah, the issue was that the scrollTop was before the Ajax request, so it first scrolled down an empty div tag and then added data. Thanks again – Root149 Nov 24 '14 at 23:44
0
$(document).ready(function() {
   $('#div').scrollTop(1000);
});
Phil
  • 1,996
  • 1
  • 19
  • 26