I am trying to write a simple toggle function in Javascript. What it does is take an element, a style name, and a desired value. If the current value of that style on the element is the empty string, that means it hasn't been set, so we set it to the given value. Otherwise, set it to the empty string to disable it.
My code is below:
function toggleStyle(el, styleName, value) {
if (el.styleName === '')
{
el.styleName = value;
}
else
{
el.styleName = '';
}
}
However, I'm unsure how I call this function if I want to toggle the visbility of a box. I know to directly change the visibility: I would normally do:
var box = document.getElementById("box");
box.style.display = "none";
But how would I call my toggleStyle to do this? I've tried writing:
toggleStyle (box, display, "none");
toggleStyle (box, style.display, "none");
toggleStyle (box.style, display, "none");
but nothing seems to work.