HTMLGoodies
The ultimate html resource
Earthweb.com


About the Double-Underlined Links


Become a Partner




Search Clipart.com:



internet.commerce















HTML Goodies : HTML and Graphics Tutorials : Forms Tutorial: TABINDEX and a Couple of Other Form Tricks

HTML GOODIES TO GO NEWSLETTER


Other Related Newsletters

TABINDEX and a Couple of Other Form Tricks


By Joe Burns

Use these to jump around or read it all...

As the Web gets bigger and bigger, the browsers become smarter and the commands get more and more complex.

After one of my previous DHTML tutorials, I received a letter from a reader who asked why I had begun writing my tutorials in a foreign language. "Don't I know it," I wrote back, "You only have to read it. I have to learn it well enough to be able to write about it."

Today is a good day because we're staying on the easy side of things. This is just about as simple as a tutorial gets. Yet, even though the command we're going to learn is easy to use, it makes such a nice effect.

I speak of TABINDEX.


The Command In Action

OK then! Let's see this pup in action. Below is a form. You'll notice that the cursor is already in the first text box. I'll tell you how I did that in a moment. Now, click the tab button on your keyboard and watch the cursor jump to the different text boxes.

Don't lie to me. You thought that was cool. I showed the effect to a couple of programming friends of mine, as well as a couple of students, and they all immediately saw the benefits. Too often you fill out forms and the elements are nicely displayed, but they're not in order, at least not in order of how they appear in the code.

Using the TABINDEX attribute, you can "force" the tab to jump to the next logical form element. How cool.

Here's how I got the effect:

<FORM NAME="joe"> <INPUT TYPE="TEXT" TABINDEX="1" VALUE="Number One" NAME="burns">
<INPUT TYPE="TEXT" TABINDEX="3" VALUE="Number Three">
<INPUT TYPE="TEXT" TABINDEX="5" VALUE="Number Five">
<INPUT TYPE="TEXT" TABINDEX="6" VALUE="Number Six">
<INPUT TYPE="TEXT" TABINDEX="4" VALUE="Number Four">
<INPUT TYPE="TEXT" TABINDEX="2" VALUE="Number Two">
</FORM>

The attribute TABINDEX is added into each of the form elements, in this case the text boxes. I then set the attributes to the numeric order I wanted the tab to follow. It works on any form element and it's easy, easy, easy.


Focus onLoad

Well, we can't leave it at that. The TABINDEX is a great attribute, but this is an HTML Goodies tutorial. It has to get difficult before we can finish.

As you know, when someone logs into a page that contains a form, the cursor does not jump to a form element right away. The user has to click on the first element in order to start filling out the form. After that, the tab key does the bulk of the work. But! Wouldn't it be nice if you could force the focus to the first form element when the user logged into the page? Why, yes. Of course it would. That's what I did above...and here's how you do it:

This goes in the BODY tag:

onLoad="self.focus();document.FORM_NAME.ELEMENT_NAME.focus()"

Even if you don't know JavaScript, you can pretty much figure this one out just by reading left to right.

  • onLoad means the following commands will be enacted when the page loads.
  • self.focus(); indicates that the focus should be placed on something in this current page. This is a method, so the empty parentheses, (), are required. Don't forget them.
  • document.FORM_NAME.ELEMENT_NAME.focus() is a hierarchy statement that forces the focus to the document, then to a specific form inside the document, and then a to specific item inside the form. That focus comes in the guise of the cursor.

To be more specific, look at the form code above. I named the form itself "joe" and the first form element "burns". Thus, the actual line I put into the page body command looks like this:

onLoad="self.focus();document.joe.burns.focus()"

You'll need to change the form and element names when you post this to your own page.

Go to page: 1  2  Next  

Tools:
Add htmlgoodies.com to your favorites
Add htmlgoodies.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

IT Management Networking & Communications Web Development Hardware & Systems Software Development Earthwebnews.com

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES