Solved

focus on html field

Posted on 2007-04-09
8
232 Views
Last Modified: 2010-04-09
hi guys

I have my jsp like this


<html>

<head>
<script language="JavaScript">
 document.forms[0].name.focus();
</script>
</head>

<body>
<form>
      <html:text  size="33"  style="fieldheight"  property="name"  />
<input type="submit"../>
...other tags
</form>
</body>
</html>

i want to put the focus on the name field as soon as the page loads, but i am getting this error
 document.forms[0].name  is null or not an object


any ideas?

thanks
J
0
Comment
Question by:jaggernat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18878888
Yes, just try to move your script at least after you have declared the element. I'm not a JSP expert and this is just a suggestion. Everything will depend on the way your servlet engine translates the page in true HTML.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18878927
>>after you have declared the element

what element??


thanks for the response
0
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18878975
I was meaning the 'name' field declaration in the form. Please apologize my Internet Explorer oriented vocabulary ;-)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:jaggernat
ID: 18879031
oh , not a problem
 



<html>
<head>
</head>
<body>
<form>
      <html:text  size="33"  style="fieldheight"  property="name"  />
<script language="JavaScript">
 document.forms[0].name.focus();
</script>
<input type="submit"../>
...other tags
</form>
</body>
</html>

i think it works if i put the javascript below(like in above code) , but i dont want to do that.  Ii want to put all the javascript on the top of the page,
any ideas??
0
 
LVL 3

Assisted Solution

by:JipFromParis
JipFromParis earned 50 total points
ID: 18879081
I'm afraid this won't be possible. The script will be executed by the browser as soon as encountered. The only exception will be functions, however you will have to trigger the function itself and will have to include some kind of script later in the document. On the other side, the script in charge of setting the focus must be executed AFTER the 'name' field has been loaded by the browser, otherwise the script will fail. Thus, there must be some kind of script below the 'name' field definition.
0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 200 total points
ID: 18879579
<script type="text/javascript">
function set_name_focus() {
  document.forms[0].name.focus();
}
</script>
<body onload="set_name_focus();">
0
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18880809
I agree with routinet. I mistakenly understood you would like the focus to be set on the name field as soon as declared and you didn't afford waiting for document load to complete.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 18883089
thank you guys
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question