Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scrolling to the top of a div

Status
Not open for further replies.

KingSlick

Programmer
Mar 9, 2007
45
US
Hello,

I have a div that I need to be a certain size. However, most of the time, there will be an overflow scroll. Within this div, I have other hidden divs. These hidden divs will be displayed on an "onClick" event. However, these same div will then be hidden again when another onClick event happens. This is working just fine. My issue is that I need for the div to be scrolled to the top on each onClick event.

Code:
function showArticle(artID) {	

	/*First check to see if the user is clicking the open article*/
	if(document.getElementById("article_" + artID).style.display == 'block') {
		document.getElementById("article_" + artID).style.display = 'none';
	} else {
		/*Close all open articles*/
		var allArticleTags=document.getElementsByTagName("*");
		var counter = 0;
		for(i=0; i<allArticleTags.length; i++) {
			if(allArticleTags[i].className == "dailyArticle") {
				counter++;
				allArticleTags[i].style.display = 'none';
			}
		}
		/*Show clicked article*/
		var scrollHeight = 1000;
		document.getElementById("article_" + artID).style.display = 'block';
		document.getElementById("content").scrollTop = document.getElementById("content").scrollTop + scrollHeight;
	}	
}

This is the code that I have so far.

Thanks in advance
-SM
 
all you need to do is set the scrollTop property of the div in question... setting it to 0 should scroll it to the top every time.
 
I interpreted "My issue is that I need for the div to be scrolled to the top on each onClick event." from the OP as saying he wanted his scrollable content div to scroll to the top with an onclick. Am I missing something?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top