harleydude Posted November 3, 2005 Share Posted November 3, 2005 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? Quote Link to comment https://forums.phpfreaks.com/topic/2795-setting-focus-to-element-when-form-loads/ Share on other sites More sharing options...
widgetapps Posted November 3, 2005 Share Posted November 3, 2005 <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] Quote Link to comment https://forums.phpfreaks.com/topic/2795-setting-focus-to-element-when-form-loads/#findComment-9346 Share on other sites More sharing options...
harleydude Posted November 3, 2005 Author Share Posted November 3, 2005 <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. Quote Link to comment https://forums.phpfreaks.com/topic/2795-setting-focus-to-element-when-form-loads/#findComment-9349 Share on other sites More sharing options...
widgetapps Posted November 4, 2005 Share Posted November 4, 2005 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] Quote Link to comment https://forums.phpfreaks.com/topic/2795-setting-focus-to-element-when-form-loads/#findComment-9365 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.