Return to Snippet

Revision: 9739
at November 19, 2008 09:53 by plindsay


Initial Code
function toggleElements() {

	// toggles the visibility of elements
	// sample usage : onclick="toggleElements('show', 'element_1', 'element_2'); etc...
	// the 1st argument is the visibility mode (show or hide)
	// other arguments are the IDs of the elements to manipulate

	if (!arguments) return false;
	if (arguments[0] != 'show' && arguments[0] != 'hide') return false;
	
	var v_mode = arguments[0];
	
	for (i = 0; i < arguments.length; i++) {
	
		var element_id = arguments[i];
		
		if (!document.getElementById(element_id)) continue; // no element with this ID exists
		
		if (v_mode == 'show') {
			document.getElementById(element_id).style.display = '';
		} else {	
			document.getElementById(element_id).style.display = 'none';
		}
	}	
}

Initial URL


Initial Description
Utility function using the DOM to toggle the visibility of multiple elements on a page.

Initial Title
toggleElements()

Initial Tags


Initial Language
JavaScript