How to set a scrollbar value

J

joe

I have a div like this:

<div id="myDiv" style="width:200; height:200; overflow:scroll;">

How do I manually set the initial vertical scrollbar position?

The div has several hundreds of lines of text and I'd like to start at a certain
position (not necessaryly text line).
 
J

Joost Diepenmaat

joe said:
I have a div like this:

<div id="myDiv" style="width:200; height:200; overflow:scroll;">

How do I manually set the initial vertical scrollbar position?

The div has several hundreds of lines of text and I'd like to start at a certain
position (not necessaryly text line).

You can set the scrollLeft and scrollTop attributes from a script.
 
J

joe

Joost Diepenmaat said:
You can set the scrollLeft and scrollTop attributes from a script.

Thanks. I tied it and it works fine in IE but not in Firefox.

My code is like this:

document.write('<div id="myDiv" style="width:2 ...blah blah
....some code
document.write('</div>');
document.getElementById('myDiv').scrollTop=650; //error here in Firefox, IE OK
 
J

Joost Diepenmaat

My code is like this:

document.write('<div id="myDiv" style="width:2 ...blah blah
...some code
document.write('</div>');
document.getElementById('myDiv').scrollTop=650; //error here in Firefox, IE OK

What error?

You can't in general count on getElementById() working before the whole
DOM is rendered. The easiest way to to work around that is to do your
document.getElementById('myDiv').scrollTop=650 call from a onload handler.
 
J

joe

Joost Diepenmaat said:
What error?

The div is not scrolled and code running stops.
You can't in general count on getElementById() working before the whole
DOM is rendered. The easiest way to to work around that is to do your
document.getElementById('myDiv').scrollTop=650 call from a onload handler.

OK. Works now. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top