Solved

focus on html field

Posted on 2007-04-09
8
230 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
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.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

776 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