Solved

focus on html field

Posted on 2007-04-09
8
227 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now