Solved

focus on html field

Posted on 2007-04-09
8
231 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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