LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2004, 08:30 AM   #1
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Rep: Reputation: 30
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.
 
Old 02-24-2004, 08:59 AM   #2
Ljohan
LQ Newbie
 
Registered: Apr 2002
Location: Holland
Distribution: SuSe 8.0
Posts: 11

Rep: Reputation: 0
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.
 
Old 02-24-2004, 09:47 AM   #3
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Original Poster
Rep: Reputation: 30
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,

Last edited by patpawlowski; 02-24-2004 at 09:49 AM.
 
Old 02-24-2004, 10:01 AM   #4
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Html Form File Cottsay General 0 11-05-2005 01:30 PM
GTK+ Related I *think* GNOME won't load, no graphical element to RedHat 9... nutnut Linux - Software 2 02-26-2005 07:33 AM
set focus on a text form of a web page? codec Programming 2 12-31-2004 04:32 AM
an html form question matt_w_lambert Programming 3 10-30-2003 11:41 PM
problem with html form meluser Linux - Software 4 03-15-2003 12:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration