Introduction
When the same page returns from the server, how do we scroll to the previously in focus element on the page? Example: If selecting a value from the dropdown placed way below on a page
refreshes the page, by default the first field will receive the focus or scroll position set to zero. It forces the user to scroll down to the previous position and causes a lot of frustration.
With simple hidden fields, we can come back to the same position and set the focus on the same field or the 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 this JavaScript code under the 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 these two JavaScript functions. You can add these JavaScript functions to the onchange
, onblur
,
or onclick
events and finally submit the page to the server.
Server Side
When you render the ASP or JSP page, you need to take both the previousScrollPosition
,
previousElement
values 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 the form fields are available in the FORM
before calling or using the JavaScript functions.
If you have multiple fields with the same name, then you need to send the index of the field to the server and get the field list using
the document.getElementsbyName("elementName")
function and loop through it to select the correct index and set the focus.
Setting focus on the next element or skipping hidden or disabled fields while doing setfocus
is a complex JavaScript action. I will show it in a different article.
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.