Activate the Tabs
The next step is to write the JavaScript routines that perform the work of keeping the tabs synchronized with the user's actions. You need to responds to three events for each tab: onClick, onMouseOver, and onMouseOut. The onClick event loads the requested panel, hides the current panel, and sets the state of the UI. The onMouseOver event highlights the current tab so the user knows that clicking on the tab will actually do something. The onMouseOut event un-highlights the tab.
When the page loads, it initializes the currentPanel variable and displays a default tab by calling the showPanel() function.
<body onLoad="showPanel(1)">
The showPanel() function does most of the work in the script. It hides the current panel, shows the requested panel, stores the index of the current panel, and finally sets the state of the UI. The hidePanel() function performs two actions: it hides and un-highlights the current panel.
The setState() function sets the current tab's background and foreground colors to their highlighted state. By including setState() in a separate function, you can make use of it later in the onMouseOut event. Essentially, setState() makes sure that the current panel is always highlighted.
The hover() function is a helper function written explicitly for the onMouseOver event. Note that it's the only function that expects an object rather than a simple index. The hover() function lets you highlight tabs without the user having to click on them.
The only remaining task is to attach the events to the page elements. You'll need events for all the possible interactions with the tabs as well as the onLoad event in the <body> element that shows the first panel:
Do you like tabbed interfaces? Did you think you had to create complicated script or use controls to implement tabbed interfaces? Can you think of ways to improve the article code? Join the discussions at Web.dhtml.general and Web.dhtml.scripting to get answers, make comments, or help others with their problems.