PDA

View Full Version : Setting focus on a non-form element?



dep
Feb 23rd, 2006, 08:12 PM
Can this be done? I'm trying to set the focus on a textbox, but i'm not using <form></form> tags, so I can't do document.formname.element.focus(); -- What's my alternative?

Thanks in advance!!

_Aerospace_Eng_
Feb 23rd, 2006, 08:17 PM
Just use document.getElementById() and stop thanking people in advance, just thank use once we've given you a solution you are happy with.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
window.onload = function()
{
document.getElementById('name').focus();
}
</script>
</head>

<body>
The cursor should be positioned on the second box.
<div><input type="text"></div>
<div><input type="text" name="name" id="name"></div>
</body>
</html>

dep
Feb 23rd, 2006, 08:18 PM
Well, i forgot to thank someone one day and they sent me this big private message about how hurt and offended they were... so.... my overly-appreciative demeanor is all thanks to him (or... her).

glenngv
Feb 24th, 2006, 12:44 PM
But form fields should be inside the form tag. Some browsers don't display fields without form tag.

_Aerospace_Eng_
Feb 24th, 2006, 12:45 PM
Though its still valid coding according to the w3 validator. Glennn do you know what browsers don't display them without form tags?

glenngv
Feb 24th, 2006, 12:51 PM
NS4. I don't know what other browsers has the same behavior. I know NS4 is old and no one uses it but it does make sense to me that form fields should be inside form tags, that's why they are called form fields or controls. You cannot submit a page without form tag.