Thursday, May 3, 2007

JavaScript - Yet another toggle (show/hide) combo box code

You probably already have found or used code to toggle 'select' elements (combo boxes). But some of them have prototype dependancies, some don't work for non-form based elements and some don't work with ajax based pages.

I just wrote this tiny code that seem to do the job on IE, both form and non-form based elements, both ajax and non-ajax based pages. Check it out:
/*
* toggle select boxes by Kaisar
*/
function toggleSelectBoxes()
{
var elems = document.getElementsByTagName("select");
if (elems)
for (i=0; i<elems.length; i++)
elems[i].style.display = elems[i].style.display == 'none' ? '' : 'none';
}

1 comment:

Ruhh said...

question from a not js user!

I want to have a long list of items. Some items can belong to multiple categories. I want to let users choose to view only the category they need.

ex: Sorting music by type

band a (country, rock)
band b (rock, jazz, experimental)
band c (rock, experimental)
band d (metal, experimental)

the main problem is that I can only use xhtml, js and a very limited bit of extra css as the site is strictly regulated.

I know this type of function must exist I just don't know where to begin looking. any guidance is greatly appreciated. my contact info is on my site. Thanks