5,448,104 members and growing! (16,271 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Scroll To previous focus element on the page

By senthil karuppaiah

Scroll To previous focus element on the page when the same page returned from the server.
Javascript, Windows, Visual Studio, Dev

Posted: 19 Aug 2006
Updated: 19 Aug 2006
Views: 13,957
Bookmarked: 6 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.41 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
4 votes, 100.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

When the same page returned from the server, scroll to previous focus element on the page. Example: If selecting a value from the dropdown placed way below on the page refresh the page, by default first field will receive the focus or scroll position set to Zero. It forces the user to scroll down to the previous position and lot of frustration.

With simple hidden fields, we can come back to same position and set the focus on same field or next field. This will allow the user to continue from the place where they left off.

Add Two Hidden fields under the FORM tag.

<input type="hidden" name = "previousElement">

<input type="hidden" name = "previousScrollPosition">

 

Add these javascripts under script tag or in your .js file.

function sendScrollPosition() {
    document.forms[0].elements["previousScrollPosition"].value = document.body.scrollTop;
    return true;
   }   
function setfocuselement(ele){
   if(document.forms[0] == null) return false;
   if(document.forms[0].elements["previousElement"] != null) {
    document.forms[0].elements["previousElement"].value=ele.name || event.srcElement.name;
   }
}
if(document.getElementsByName("previousScrollPosition").length > 0) {
  document.body.scrollTop = document.forms[0].elements["previousScrollPosition"].value;
}
var prvElement= document.forms[0].elements["previousElement"];
if(prvElement != null && prvElement.value != null ) {
       document.forms[0].elements[prvElement.value].focus();

}

When you submit the page, you need to call those two javascripts. You can add those javascript function to onchange or onblur or onclick events and finaly submit the page to server.

Server Side

When you render the ASP or JSP page, you need to take both previousScrollPosition, previousElement value from the Request object and populate both the hidden fields.

<form action="demo_form2.asp" method="post">
First name:
<input type="text" name="name" value="Donald" />
<br />
Last name:
<input type="text" name="name" value="Duck" />
<br />

<input type="hidden" name = "previousElement" value="<%Request.Form("previousElement")%>"> 
<input type="hidden" name = "previousScrollPosition" value="<%Request.Form("previousScrollPosition")%>">
<input type="submit" value="Submit" />
</form>

You need to make sure that form fields available in the FORM before calling or using the javascript.

If you have multiple fields with same name, then you need to send the index of the field to the server and get the field list using document.getElementsbyName("elementName") function and loop through them to select correct index and set the focus.

Setting  focus on next element or skiping hidden or disabled fields while doing setfocus is a complex javascript. I will show it in different article.

 








			
			
			

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

senthil karuppaiah


Twelve years of progressive experience in Software Product Development, tactical planning, project and client management, demonstrated success in leadership, critical thinking, problem solving and analysis. Diverse knowledge and experience with multiple development methodologies, reengineering, software engineering, and integration of heterogeneous systems


Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralPosition scrollbar of a divmember1:24 2 Feb '07  
Generalcrossbrowsermemberueli.kunz21:37 23 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Aug 2006
Editor:
Copyright 2006 by senthil karuppaiah
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project