Click here to Skip to main content
6,635,160 members and growing! (18,970 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

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
Views:19,906
Bookmarked:8 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 2.41 Rating: 4.00 out of 5

1

2

3
4 votes, 100.0%
4

5

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=&quot;hidden&quot; name = &quot;previousElement&quot;>

<input type=&quot;hidden&quot; name = &quot;previousScrollPosition&quot;>

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

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

if(document.getElementsByName(&quot;previousScrollPosition&quot;).length > 0) {


    document.body.scrollTop = document.forms[0].elements[&quot;previousScrollPosition&quot;].value;
}

var prvElement= document.forms[0].elements[&quot;previousElement&quot;];

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=&quot;demo_form2.asp&quot; method=&quot;post&quot;>
First name:
<input type=&quot;text&quot; name=&quot;name&quot; value=&quot;Donald&quot; />
<br />
Last name:
<input type=&quot;text&quot; name=&quot;name&quot; value=&quot;Duck&quot; />
<br />

<input type=&quot;hidden&quot; name = &quot;previousElement&quot; value=&quot;<%Request.Form(&quot;previousElement&quot;)%>&quot;> 
<input type=&quot;hidden&quot; name = &quot;previousScrollPosition&quot; value=&quot;<%Request.Form(&quot;previousScrollPosition&quot;)%>&quot;>
<input type=&quot;submit&quot; value=&quot;Submit&quot; />
</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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

senthil karuppaiah


Member
Fourteen 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
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralPosition scrollbar of a div Pinmember1:24 2 Feb '07  
Generalcrossbrowser Pinmemberueli.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: Sean Ewington
Copyright 2006 by senthil karuppaiah
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project