Solved

Div auto scroll to bottom

Posted on 2010-09-11
13
705 Views
Last Modified: 2012-05-10
Ive been trying to get a Div to load, and scroll to the bottom. The idea is to have a list of transactions, and have the div scroll to the bottom.

I found an article at 'http://radio.javaranch.com/pascarello/2005/12/14/1134573598403.html', and tried to put it in the onload of the div, but it doesnt scroll to the bottom. It doesnt produce any errors, just doenst work :-(

Any idea what Im doing wrong?

Thanks in advance!!!


<div id="divTransList" style="width:100%; height:150px; overflow:auto;" onload="javascript: var objDiv = document.getElementById('divTransList'); objDiv.scrollTop = objDiv.scrollHeight;">
        hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>hello</br>
    </div>

Open in new window

0
Comment
Question by:tonelm54
13 Comments
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 33652966
The given page cannot be found!
0
 

Author Comment

by:tonelm54
ID: 33652970
Sorry, its including the ' at the end of the hyperlink.

The url is:-
http://radio.javaranch.com/pascarello/2005/12/14/1134573598403.html
0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 33652994
One, you have a syntax error "" should be typed "" and two anchor points use the name attribute, check out the following code:
<div id="divTransList" name="divTransList" style="width:100%; height:150px; overflow:auto;">

hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />

</div>

<script type="text/javascript">
window.onload= function event(onload) {window.location = "#divTransList"};
</script>

Open in new window

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 4

Expert Comment

by:antonybrahin
ID: 33652996
put the script in a function and call it after the div loads.
0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 33652998
Sorry:  ->
0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 33653004
It takes br and interpretes it as real code, the point is you have mistyped it.
0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 33653011
Like this:



hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello



<script type="text/javascript">
function jumpToAnchor (anchorPoint) {
      window.location = "#" + anchorPoint;
}

jumpToAnchor("divTransList");
</script>
0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 33653013
Like this:
<div id="divTransList" name="divTransList" style="width:100%; height:150px; overflow:auto;">

hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />

</div>

<script type="text/javascript">
function jumpToAnchor (anchorPoint) {
	window.location = "#" + anchorPoint;
}

jumpToAnchor("divTransList");
</script>

Open in new window

0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 33653023
I just saw the page let me change the code a little bit.
0
 
LVL 2

Accepted Solution

by:
kostantinos1995 earned 500 total points
ID: 33653045
I'm sure this is what you want:
<div id="divTransList" name="divTransList" style="width:100%; height:150px; overflow:auto;">

</div>

<script type="text/javascript">
function addTimeToDIV () {
	objDiv= document.getElementById('divTransList');
	// Your code goes here
	objDiv.innerHTML += new Date().getTime() + "<br />";
	// and ends here
	objDiv.scrollTop= objDiv.scrollHeight;
}

setInterval ("addTimeToDIV()", 500 );
</script>

Open in new window

0
 
LVL 14

Expert Comment

by:sam2912
ID: 33653540
if you only want the div to jump to the bottom once, on page load, do this:
<script type="text/javascript">
window.onload = function() {
   var elem = document.getElementById('divTransList');
   elem.scrollTop = elem.scrollHeight;
}
</script>

Open in new window

0
 
LVL 2

Expert Comment

by:sqlgang
ID: 33656433
Code for IE:

<html>
<body>
    <script language="javascript" type="text/javascript">
        function MoveDivToBottom() {
            var obj = document.getElementById("divTransList");
            document.getElementById("foodiv").innerHTML = document.getElementById("foodiv").innerHTML + "::" + obj.scrollHeight + ">>" + obj.offsetHeight;
            obj.scrollTop = obj.scrollHeight;
        }
    </script>
<div id="foodiv" style="display:none"></div>    
    <div id="divTransList" style="width: 100%; height: 150px; overflow: auto;">hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />
        hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello
        <br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />hello<br />
        hello<br />hello<br />hello        End
</div><script language="javascript" type="text/javascript">MoveDivToBottom();</script>

</body>
</html>

Open in new window

0
 
LVL 2

Expert Comment

by:sqlgang
ID: 33656438
For Non IE: You may remove the foo div and its innerHTML manipulation from the MovDivToBotom - as it is specific for the IE only where it needs some time to move to bottom - which it gets by doing some funny stuff.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now