Trying to get a better understanding of JavaScript. I've checked out a few similar questions answered on this forum but I'm still confused on this point.
I've got some code off the web that toggles the visibility of a modal window (div) and an overlay (div) - (I know this might be better done with jQuery but I wanted to understand JS first):
function overlay() {
el = document.getElementById("overlay");
/* TERNARY */
el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
/*
if(el.style.visibility == "visible"){
el.style.visibility = "hidden";
}else if(el.style.visibility == "hidden"){
el.style.visibility = "visible";
}
*/
}
I thought that the ternary operator was just a more compact way of writing an if/else statement. But when I substitute the ternary operator in the code above with an if/else statement (currently commented out in code), the code doesn't work.
I've probably got something wrong but I can't figure out what? Could someone help?
Thanks.
ifstatement. Instead, it's an "inline"if/elseexpression.