Website designers often use JavaScript to expose dynamic content to visitors. Sadly, updating JavaScript can be painful at best. CSS can help to overcome JavaScript's limitations -- and the two together open up some lovely possibilities. Alejandro Gervasio explains, with dynamic examples.
Preloading HTML Content with CSS - Building a simple drop-down menu with CSS (Page 5 of 6 )
So far, we‘ve shown a couple of examples to clearly demonstrate how powerful and handy this hiding technique can be for generating on demand content. It would be good to expand out those capabilities to build up different applications, exposing the whole gamut of possibilities when using CSS to hide Web page elements. So, let’s start increasing our creativity by creating a simple drop-down menu, which is very suitable for our sample needs.
The basic logic to creating the drop-down menu consists of hiding and displaying selected <div> elements using the CSS display property, accordingly to the occurrence of an event -- generally, when the user clicks on a specific area or passes the mouse over it. Having defined the general concept, all we have to do is create our menu. As usual, here is the code for our JavaScript functions:
<script language="javascript"> // loads global functions loadGlobalFunctions=function(){ // switches on-off menu display switchMenu=function(){ menu=document.getElementById('menu'+this.id); if(menu.className=='hidden'){ menu.className='menu'; } else{ menu.className='hidden'; } } // gets all <a> elements as=document.getElementsByTagName('a'); navAs=[]; // makes array from <a> elements with class name 'navbar' for(i=0;i<as.length;i++){ if(/\bnavbar\b/.test(as[i].className)){ navAs[navAs.length]=as[i]; // assigns onclick event handler to navbar <a> elements navAs[i].onclick=switchMenu; } } } // executes code once page is loaded window.onload=loadGlobalFunctions; </script>