LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   HTML setting focus on a form element on load (https://www.linuxquestions.org/questions/programming-9/html-setting-focus-on-a-form-element-on-load-149922/)

patpawlowski 02-24-2004 08:30 AM

HTML setting focus on a form element on load
 
I'm looking for a way to set the focus on a text input box on an HTML form. The page is used for adding email addresses to our email list. When I have a stack of emails to add, it would be nice to not have to grab the mouse everytime the page reloads and click the text box. I didn't know if this could be done using HTML or if I need to turn to JS.

Ljohan 02-24-2004 08:59 AM

I think you should do something like:


Code:

<html>
  <head>
    <script>
      <!--
        function sf(){document.formName.objectToFocus.focus();}
      // -->
    </script>
  </head>
  <body onLoad=sf()>

  Rest of the HTML code.

This should do the trick if you make sure that the formName and the objectToFocus are set correct.

patpawlowski 02-24-2004 09:47 AM

Cool, worked like a charm. I wish more sites would use this. There is one problem in my case. The body tag is in a separate document that is included in every page on our site. Is there any other event that I could use to trigger the function? I'm still learning JS and couldn't come up with anything else that's automatic. (Actually I am working on an online JS tutorial right now in another browser window;) It's working right now, but it's calling the function on every page on our site, which isn't a problem but I might like to modify it to focus different elements on different pages.

Thanks,

luxitan 02-24-2004 10:01 AM

just put the script at the end of your page, but out of the function, ie:
<script language...>
document.formName.objectToFocus.focus();
</script>

just put after you declare the object that needs the focus


All times are GMT -5. The time now is 02:57 AM.