Hi! I have a menu in js where I have 4 divs. Element 1 is the div that should appear, elements 2-3 are the ones that should hide or stay hidden in case already hidden. The function works. Now I have situations where I only have 3 elements total. So 1 that should be visible and 2 that should hide. How can I use the same function to do that? So how to make the function below run normally even if I do not input element4. Thank you for your help! function displayElement(element1,element2,element3,element4){ var element1 = document.getElementById(element1); var element2 = document.getElementById(element2); var element3 = document.getElementById(element3); var element4 = document.getElementById(element4); element2.style.display = 'none'; element3.style.display = 'none'; element4.style.display = 'none'; if (element1.style.display == 'none') element1.style.display = ''; else element1.style.display = 'none'; } Code (markup):
displayElement = function() { var elements = Array.prototype.slice.call(arguments); var element1 = document.getElementById(elements.shift()).style; element1.display = element1.display == 'none' ? '' : 'none'; foreach(el in elements) document.getElementById(elements[el]).style.display = 'none'; }; Code (markup): untested, but something like that should work.