AndrewAK
04-11-2004, 11:03 AM
I've got this form. So far, I can loop through it and check each element to see if it's blank. I want to set the focus to the last element. Example: Message "txtOne is Blank". After clicking OK I want the focus to return to, in this case, to txtOne. In short, I want to set the focus to the object that is indicated in the last message/alert.
Here is the code.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
function setFocus_onload()
{
document.form1.txtOne.focus();
}
function checkBlank_onclick()
{
var i;
var myform = document.form1;
for (i = 0; i <= 4; i++)
{
if(myform.elements[i].value == "")
{
alert(myform.elements[i].name + " is blank.")
}
}
}
</script>
</head>
<body>
<form action="confirm.asp" method="post" name="form1">
Item1<input name="txtOne" type="text" /><br />
Item2<input name="txtTwo" type="text" /><br />
Item3<input name="txtThree" type="text" /><br />
Item4<input name="txtFour" type="text" /><br />
<input name="submit" type="button" value="Sumbit" onclick="checkBlank_onclick()"/><br />
<input name="reset" type="button" value="Reset" />
</form>
</body>
</html>
Here is the code.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
function setFocus_onload()
{
document.form1.txtOne.focus();
}
function checkBlank_onclick()
{
var i;
var myform = document.form1;
for (i = 0; i <= 4; i++)
{
if(myform.elements[i].value == "")
{
alert(myform.elements[i].name + " is blank.")
}
}
}
</script>
</head>
<body>
<form action="confirm.asp" method="post" name="form1">
Item1<input name="txtOne" type="text" /><br />
Item2<input name="txtTwo" type="text" /><br />
Item3<input name="txtThree" type="text" /><br />
Item4<input name="txtFour" type="text" /><br />
<input name="submit" type="button" value="Sumbit" onclick="checkBlank_onclick()"/><br />
<input name="reset" type="button" value="Reset" />
</form>
</body>
</html>