Jump to content

Setting focus to element when form loads


Recommended Posts

I have a basic login form with a row of menu buttons at the top of the page, 2 text elements (login name and password) and a logon button. What I want to do is default the cursor (set focus) to the login name text box whenever the form is opened OR if the page is refreshed.

 

How do I do this?

Link to comment
Share on other sites

<body onload="document.formName.fieldName.focus()">

 

I have a basic login form with a row of menu buttons at the top of the page, 2 text elements (login name and password) and a logon button. What I want to do is default the cursor (set focus) to the login name text box whenever the form is opened OR if the page is refreshed.

 

How do I do this?

314516[/snapback]

 

Link to comment
Share on other sites

<body onload="document.formName.fieldName.focus()">

314521[/snapback]

 

Thanks you for the help, but...

 

This page is heavily PHP driven and I am unsure about how to impliment this, especially since the <BODY...> tag is within a template and used throughout the site. I should be able to use php logic to tell the page to use it or not, that is not the problem, what I need is information on using this function? Also, is this PHP safe? I may be totally offbase inthese questions, but this is an active production system and I need to be sure about changing any global setting.

 

Thanks in advance for your help.

Link to comment
Share on other sites

Another option that should work, is to embed the Javascript inside the body, but AFTER the HTML form. That way the JS is treated as part of the content of your template. It needs to be after the form so the browser runs it after the form is parsed. If you put it before, you might run into undefined errors (depends on browser/versions).

 

To add this kind of functionality into a template engine is a bit more work, and much harder to explain on a forum.

 

<script language="JavaScript" type="text/javascript">
document.formName.fieldName.focus();
</script>

 

Thanks you for the help, but...

 

This page is heavily PHP driven and I am unsure about how to impliment this, especially since the <BODY...> tag is within a template and used throughout the site. I should be able to use php logic to tell the page to use it or not, that is not the problem, what I need  is information on using this function? Also, is this PHP safe? I may be totally offbase inthese questions, but this is an active production system and I need to be sure about changing any global setting.

 

Thanks in advance for your help.

314556[/snapback]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.