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
https://forums.phpfreaks.com/topic/2795-setting-focus-to-element-when-form-loads/
Share on other sites

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

 

  Quote
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]

 

  Quote
<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.

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>

 

  Quote
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.