You are here: Focus on element not working correctly « HTML « IT news, forums, messages
Focus on element not working correctly

Posted by Adrienne Boswell on 10/03/07 23:43

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%>
<!--#include file="docheader_inc.asp"-->
<%
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
%>
<script type="text/javascript">
alert("<%=message%>");
document.getElementById("message").focus()
</script>
<% message = "<div id=" & chr(034) & "message" & chr(034) &
"><strong>" & message & "</strong></div>"

end if

fname = "Your first name"

end if%>
<style type="text/css">
..required, #required {
font-weight:bold;
color:red;
}
<% if required <> "" then%>
#<%=required%>1 {
font-weight:bold;
background-color: yellow;
color:red
}
#<%=required%> {
background-color: pink;
color: black
}
<% end if%>
</style>
</head>
<body>
<form method="post" action="<%=request.servervariables("script_name")
%>">
<%=message%>
<fieldset><legend>* indicates <span class="required">Required</span>
fields</legend>
<label for="fname" id="fname1" class="required">First Name </label>
<input type="text" name="fname" id="fname" value="<%=fname%>" <%if
isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
%end if %>><br>
<label for="lname" id="lname1">First Name </label> <input type="text"
name="lname" id="lname" value="<%=lname%>" <%if isget then
%>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
%>><br>
</fieldset>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация