Rayne
Rayne

Reputation: 32645

Is there a generally cross-browser way to use JavaScript to make a div scroll all the way down?

It looks like this:

var objDiv = document.getElementById("code");
objDiv.scrollTop = objDiv.clientHeight;

works in Chrome, but not FF, and this:

var objDiv = document.getElementById("code");
objDiv.scrollTop = objDiv.scrollHeight;

works in FF, but not Chrome.

Is there a better all around way to do this?

Upvotes: 1

Views: 516

Answers (3)

KooiInc
KooiInc

Reputation: 122956

Try window.scrollTo(0,objDiv.offsetTop+objDiv.offsetHeight);

Upvotes: 1

James
James

Reputation: 111960

objDiv.scrollTop = 100000; // or some other big number less than 2147483647

Upvotes: -1

Pekka
Pekka

Reputation: 449693

How about placing an anchor at the very bottom of the div, and scrollIntoView() into it? That should work cross browser.

Upvotes: 4

Related Questions