Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't set focus()

Status
Not open for further replies.

awingnut

Programmer
Joined
Feb 24, 2003
Messages
759
Location
US
I'm trying to set the initial focus to a field in a form when it loads. In the <head> I have the following:
Code:
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
	<!--
         window.document.ProposalForm.Required.focus();
	//-->
</script>
The name of the form is 'ProposalForm' and the name of the field is 'Required'. However, no field visably has focus when the page is loaded (there are a couple of buttons but I don't see them highlighted as if they have focus either). Can someone spot my error? TIA.
 
you need to call this... it will not happen just because it's there... try something like:

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
<!--
function focMu() {
window.document.ProposalForm.Required.focus();
}
//-->
</script>

<body onLoad=&quot;focMe();&quot;>

[conehead]
 
Oh, I thought scripts in the <head> were executed on load unless it is a 'function'. Thanks, I'll try it.
 
this should work as well -

<body onLoad=&quot;javascript:window.document.ProposalForm.Required.focus();
&quot;>



[conehead]
 
>> Oh, I thought scripts in the <head> were executed on load unless it is a 'function'

Everything in your script tags is interpreted by the browser as it reads it. You are correct in that if you have [tt]alert('boo!');[/tt] in your script, you will get a 'boo!' prompt as the page loads (note: AS the page loads - as opposed to the onload which occurs after the page has loaded.

The reason why [tt]window.document.ProposalForm.Required.focus();[/tt] alone in the script won't work is that the browser tries to run it as soon as it gets to it. Seing as how it's at the top of the page when it sees the script, there is no 'ProposalForm' yet.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Also, you might consider something like the following (it uses the W3C DOM [document object model], and is basically more future-proof :-) ):
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>focusOn test</title>
<script>
function focusOn(strID){
	var fieldToFocus = document.getElementById(strID);
	fieldToFocus.focus();
}
</script>
</head>
<body onload=&quot;focusOn('focusID');&quot;>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<input type=&quot;text&quot; name=&quot;focusField&quot; value=&quot;got focus?&quot; id=&quot;focusID&quot;>
</body>
</html>

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thanks for all the info. It sure helps to have a better understanding of what is going on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top