I changed my code a bit and now it works.
<body> <span style="visibility: hidden;">A</span> <span>X</span> <span style="visibility: hidden;">B</span> <span>Y</span> <span style="visibility: hidden;">C</span> <span>Z</span> </body>
$(function() { var elems = document.getElementsByTagName('span'); for (var i = 0; i<elems.length; i++) { if (elems[i].style.visibility == 'hidden') { elems[i].style.visibility = 'visible'; } else { elems[i].style.visibility = 'hidden'; } } });
The problem is that you are using the html hidden attribute, but you are changing the css hidden attribute. I modified your HTML code to set hidden as css property, not HTML property. Also, I switched the if else block and it worked. I think style.visibility is not initialized until you give it meaning. It is null , not visible .
source share