Focus on element not working correctly

Focus on element not working correctly

am 04.10.2007 01:43:08 von Adrienne Boswell

Here is the scenario. I have an alert that fires if the user has not
filled in a field. What I would like to do is have the browser focus
on the message (div id="message"). That div is generated only if
there is an error, and the server side code is placed within the
head. What I think is happening is the js focus is firing BEFORE the
div is rendered, because I am getting the js error
document.getElementById("") is null or is not an object. This is not
something vital, just something I think would be nice for the user
(especially if the form and message are below the fold). I have
included code and markup below. (no URL - this is stripped down to
what's necessary).

<% option explicit%>

<%
dim fname, isget

if request.servervariables("REQUEST_METHOD") = "POST" then
ispost = true
elseif request.servervariables("REQUEST_METHOD") = "GET" then
isget = true
end if

if ispost then %>
if request.form("fname") = "" then
required = "fname"
message = "First name is required"
end if

if message <> "" then
%>

<% message = "

">" & message & "
"

end if

fname = "Your first name"

end if%>



%>">
<%=message%>
* indicates Required
fields


isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
%end if %>>

name="lname" id="lname" value="<%=lname%>" <%if isget then
%>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
%>>