/ Published in: JavaScript
Utility function using the DOM to toggle the visibility of multiple elements on a page.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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'; } } }