How to set a scrollbar value

Discussion in 'Javascript' started by joe, Apr 5, 2008.

  1. joe

    joe Guest

    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).
    joe, Apr 5, 2008
    #1
    1. Advertising

  2. joe <> writes:

    > 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.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Apr 5, 2008
    #2
    1. Advertising

  3. joe

    joe Guest

    Joost Diepenmaat <> wrote:

    >joe <> writes:
    >
    >> 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.


    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
    joe, Apr 6, 2008
    #3
  4. joe <> writes:


    > 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.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Apr 6, 2008
    #4
  5. joe

    joe Guest

    Joost Diepenmaat <> wrote:

    >joe <> writes:
    >
    >
    >> 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?


    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.
    joe, Apr 7, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moonhk
    Replies:
    5
    Views:
    541
    moonhk
    Oct 11, 2006
  2. Alex

    ScrollBar? Does it exist just WEB ScrollBar Control?

    Alex, Apr 1, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    265
    Alvin Bruney [MVP]
    Apr 4, 2004
  3. femina

    How to set color for scrollbar in a Panel

    femina, Sep 23, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    165
    femina
    Sep 23, 2005
  4. Danny & Noemi Shafir

    Set a value in the opener window doesn't really set it?

    Danny & Noemi Shafir, Sep 10, 2006, in forum: Javascript
    Replies:
    0
    Views:
    112
    Danny & Noemi Shafir
    Sep 10, 2006
  5. joe
    Replies:
    4
    Views:
    104
Loading...

Share This Page