473,473 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

scrolling a div

I have a simple page where the user adds a comment to a form that gets saved
in a database, the page refreshes and reads the contents of the database and
displays them inside a scrolling div.

The following piece of code simulates some text inside a div where the text
is longer than the div is high. I would like to force the display to the end
of the - as if the user had scrolled to the end - when the page refreshes.

What is the property of the div that controls the position of the text
inside it and how do I set this to the end with javascript? IF this was a
text area then I could just say:

document.all.Comments.scrollHeight = 10000;

I think. I am using a div instead of a text area because the text I am
displaying will contain html.

Thanks!

<div id="Comments" style="background-color: #FFFFDD; border: inset 2px
#999999; overflow:auto; width=200; height=100;">
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
</div>


Aug 28 '05 #1
3 2722


Simon Wigzell wrote:

What is the property of the div that controls the position of the text
inside it and how do I set this to the end with javascript?


IE introduced the properties
scrollTop
scrollLeft
scrollHeight
scrollWidth
for element objects, see
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/scrolltop.asp>
so in browser that support that you can do
if (typeof element.scrollTop != 'undefined' &&
typeof element.scrollHeight != 'undefined')
{
element.scrollTop = element.scrollHeight;
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 28 '05 #2
<snip>

Thanks, that is exactly what I needed.
Aug 28 '05 #3
Simon Wigzell wrote :
I have a simple page where the user adds a comment to a form that gets saved
in a database, the page refreshes and reads the contents of the database and
displays them inside a scrolling div.

The following piece of code simulates some text inside a div where the text
is longer than the div is high. I would like to force the display to the end
of the - as if the user had scrolled to the end - when the page refreshes.

What is the property of the div that controls the position of the text
inside it and how do I set this to the end with javascript? IF this was a
text area then I could just say:

document.all.Comments.scrollHeight = 10000;

1- scrollHeight is a read-only property. You can't just set it to an
arbitrary value.

2- as written your code will fail in many browsers

Try instead

document.getElementById("Comments") when accessing that div.

http://www.mozilla.org/docs/web-deve...tml#dom_access
I think. I am using a div instead of a text area because the text I am
displaying will contain html.

Thanks!

<div id="Comments" style="background-color: #FFFFDD; border: inset 2px
#999999; overflow:auto; width=200; height=100;">
<div id="Comments" style="background-color: #FFFFDD; border: inset 2px
#999999; overflow:auto; width: 200px; height: 100px;">

Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
Some Text<br>
</div>


document.getElementById("Comments").scrollTop =
document.getElementById("Comments").scrollHeight -
document.getElementById("Comments").clientHeight;

Gérard
--
remove blah to email me
Aug 28 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Midas NDT Sales | last post by:
I have been looking at a simple example of a scrolling text box (the one in the SAM book) as below: <script language="JavaScript"> var pos=100; function Scroll() { if...
1
by: Kirk | last post by:
Is it possible to stop horizontal scrolling whilst still maintaining vertical scrolling within an <IFRAME> ? Within each page of my site I have an IFRAME for content. For some pages there is...
44
by: Jim M | last post by:
I have had great success with using <iframe> with overflow-y set to auto. I can get a similar look with the <iframe> tag. BUT... In all cases I need to have fixed heights. Is there a way to...
1
by: Tim Frawley | last post by:
I have a large form with many controls, almost like legal size paper in height. Just so I can head these off, I am not looking for suggestions to the effect of using Tab controls, multi-window...
2
by: P2P | last post by:
Hi I am wondering if someone know of a free cross-browsers vertical scrolling script that - is cross cross-browsers - will call the scrolling content from an external html page or from a...
4
by: Keith Bentrup | last post by:
Hi all, I wrote a simple search function to find text in a textarea where not all the text is visible (ie. the text box displays 10 lines but there may be more than 1000 lines to search). I can...
1
by: atif | last post by:
Hi, I m new to CSharp.. so may be my preliminary questions look stupid.. but plz help me.. I want to drag controls like buttons or some other user controls over form.. i have set form's...
5
by: PythonistL | last post by:
I am a newbie with Javascript. I have this simple script for scrolling text <HTML> <HEAD> <TITLE>Scrolling Message Script</TITLE> <SCRIPT language="JavaScript"><!-- var msg = 'My scrolling...
3
by: Chamnap | last post by:
Hello everybody, I have one problem. I want to do something after the user finished scrolling. The scroll event fires whenever the user is scrolling. I don't want this actually. Does anyone has...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.