Jump to content


Photo

Setting focus to element when form loads


  • Please log in to reply
3 replies to this topic

#1 harleydude

harleydude
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 03 November 2005 - 06:58 PM

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?
Success = 1% inspiration + 99% persperation!
Support PHP Freaks! TODAY!

#2 widgetapps

widgetapps
  • Members
  • Pip
  • Newbie
  • 8 posts
  • LocationToronto

Posted 03 November 2005 - 07:15 PM

<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?

View Post



#3 harleydude

harleydude
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 03 November 2005 - 08:31 PM

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

View Post


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.
Success = 1% inspiration + 99% persperation!
Support PHP Freaks! TODAY!

#4 widgetapps

widgetapps
  • Members
  • Pip
  • Newbie
  • 8 posts
  • LocationToronto

Posted 04 November 2005 - 02:06 PM

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.

View Post






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users