Jan 5 2006, 07:24 PM
Post
#1
|
|
![]() Boss, my code's compiling (xkcd) Group: Admins Posts: 10,440 Joined: 19-September 01 Member No.: 1 ![]() |
Creating collapsible elements using JavaScript and CSS
Fancy version: http://www.cyberiapc.com/writings/2006_1.php 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 browser How 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:
CODE #search_options { display: none } 2. The HTML file displays the table and binds the CSS formatting and JavaScript logic to it. CODE <form name='frmSearch' action='' method='post'> <table style='width:700px;border:1px solid #000000' cellpadding='3' cellspacing='3'> <tr><td width='100%'> <input type='text' name='searchterm' maxlength='128' style='width:99%' /> </td> <td> <a href='#' onclick="return hidify_showify('search_options','search_arrow','Less','More');"> <img src='images/arrow_down.gif' alt='Advanced Options' border='0' id='search_arrow' /> </a> </td></tr> <tr><td width='100%' id='search_options'> <br />__Advanced stuff goes here__<br /> </td></tr> <tr><td width='100%' align='center'> <input type='submit' name='submit' value='Search' /> </td></tr> </table> </form> 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. CODE <script type="text/javascript"> function hidify_showify(e_table, e_img, alt_less, alt_more) { if(document.getElementById) { var id_table = document.getElementById(e_table).style; var id_img = document.getElementById(e_img); //Set the object to table-cell if the browser is //Firefox and block if it's anything else. if(navigator.userAgent.indexOf("Firefox") != -1){ if(id_table.display == "table-cell") { id_table.display = "none"; id_img.src = "images/arrow_down.gif"; id_img.alt = alt_more; } else { id_table.display = "table-cell"; id_img.src = "images/arrow_up.gif"; id_img.alt = alt_less; } } else { if(id_table.display == "block") { id_table.display = "none"; id_img.src = "images/arrow_down.gif"; id_img.alt = alt_more; } else { id_table.display = "block"; id_img.src = "images/arrow_up.gif"; id_img.alt = alt_less; } } return false; } else { return true; } } </script> 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. -------------------- ![]() ![]() Things that I don't suck at: Photography (flickr, JPG Mag), "Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com |
|
|
|
madmo ali, thank you but interesting issue with mozilla.... Jan 6 2006, 02:04 PM
usr.c hmm...The first block needs to check for Firefox a... Jan 6 2006, 03:44 PM
madmo lol , fair enough just out of interest would you k... Jan 7 2006, 01:33 PM
usr.c Fixing it is a bit less straightforward than I had... Jan 6 2006, 09:54 PM
usr.c Don't worry, it'll have to be fixed since ... Jan 7 2006, 04:06 PM
usr.c The demo should work properly now in Mozilla. I... Jan 8 2006, 05:57 PM
usr.c I forgot to post the updated code.
This is what I... Jan 12 2006, 04:42 PM
Guest Hi, brilliant script, is there any way to add mult... Feb 6 2006, 08:22 PM
Guest QUOTE(Guest @ Feb 6 2006, 08:22 PM) Hi, b... Feb 7 2006, 01:03 PM
Matthew QUOTE(Guest @ Feb 7 2006, 01:03 PM) I hav... May 11 2007, 06:47 AM
Chris-McC QUOTE(Matthew @ May 11 2007, 06:47 AM) Ca... Jul 6 2007, 03:09 PM
Mr Enjoy QUOTE(Chris-McC @ Jul 6 2007, 03:09 ... Sep 26 2007, 11:15 AM
usr.c Glad you managed to fix it, and thanks for your co... Feb 10 2006, 12:53 AM
BlueNC Awesome script, Thank you very much for making it ... Jun 8 2006, 03:31 AM
usr.c Thanks for your comments.
How about if you add ... Jun 8 2006, 02:20 PM
BlueNC QUOTE(usr.c @ Jun 8 2006, 02:20 PM) Thank... Jun 8 2006, 05:46 PM
BlueNC Hi,
one more question and I hope you might be abl... Jun 13 2006, 02:58 AM
usr.c Sure. Expect a reply from me during the weekend (... Jun 15 2006, 01:29 PM
usr.c I promise to get back to you on that in a few days... Jul 8 2007, 06:06 PM
usr.c I should have read your post more closely back whe... Jul 13 2007, 06:52 PM![]() ![]() |
|
Lo-Fi Version | Time is now: 25th May 2013 - 03:19 AM |