Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

focus on html field

Posted on 2007-04-09
8
Medium Priority
?
235 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 200 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 51

Accepted Solution

by:
Steve Bink earned 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

670 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