|
|
|
Ali Almossawi, January 5, 2006
I woke up from a long sleep, had a stretch, rubbed my eyes and looked at the computer screen. “Damn”, I thought, I need to learn some of that cool stuff that Web developers are doing these days. It’s still a long way to Ajax-nirvana, so I’m hoping to take things one step at a time. This article will show you how I created a simple collapsible table for a project that I work on in my free time. What you need: An editor and a browserHow long will it take: Less than 10 minutes Skill level: Beginner The goal is to create a search box that allows the user to choose whether or not to show advanced search options. Search boxes in some desktop applications work this way. It’s a nice concept because it reduces on-screen clutter. There are three parts to the implementation:
1. The CSS rule uses the display property to determine whether the element implementing it is displayed or hidden. It is set to none by default meaning that the element is hidden when it’s first loaded. #search_options {
display: none
}
2. The HTML file displays the table and binds the CSS formatting and JavaScript logic to it. Notice that the table has three rows. The first shows a text box for typing the query and a button for switching between the table’s collapsed and expanded states. The second row contains the advanced options that will be shown or hidden on demand. The third shows our submit button. The interesting parts are shown in bold. Clicking on the More/Less image calls the JavaScript function hidify_showify(). More about that later. The second table row has search_options as its ID, which is defined in the CSS as being hidden by default. The More/Less image is also given an ID, though it's just so that we can refer to it later on and change its source and alt tag depending on whether the table is collapsed or expanded. 3. The JavaScript function contains the logic that manipulates the CSS rule and switches its display property. It also checks the browser type and uses the value for display that is supported by that browser. Firefox doesn’t support the value table-cell, but IE does. If block or table-cell is always used then the contents of the table won't look too good in one of the browsers. You can surely optimize the code shown below by reducing its redundent parts, but I think the way it looks now is intuitive to understand. Note that because we're passing IDs to this function, we use document.getElementById() to get the actual elements that we can then manipulate. That’s it. Clicking on the More/Less image invokes the JavaScript function, which then looks at the elements identified as search_options and search_arrow and decides what to set their CSS properties to. This file contains all the code mentioned here with the CSS, JavaScript and HTML defined seperately. You can take a look at http://cyberiapc.com/zenith_demo/search.php to see this code in action. |