Problem:
I am making a simple demo site to play around with JavaScript and I'm getting a "TypeError: document.getElementById(...) is null"
when trying to change an elements display value to block upon clicking a button.
Seems simple enough but the error is persistent. I have tried the suggested solutions of using window.onload or placing my script in the bottom of the body tag but nothing helps. Can anyone help me out and point out what i might be doing wrong? (P.S I'm 2 days into learning JS)
<nav class="middle"> <ul> <li><a href="#intro" onclick="buttons()">Intro</a></li> <li class="line">|</li> <li><a href="#about" onclick="buttons()">About</a></li> <li class="line">|</li> <li><a href="#contact" onclick="buttons()">Contact</a></li> <!--<li><a href="#elements">Elements</a></li>--> </ul> </nav> <div id="about"> <header id="header2"> <h1>Farhan Chandel</h1> <h3> Software Systems Major</h3> </header> </div>
JS Code
function buttons() {
document.getElementById(about).style.display="block"; }
My apologoies, the 'name' in the brackets was from a previous different attempt at solving the problem. I have changed it back to the original problem.
More Clarification: The 'about' div has a display style of none; and upon clicking one of the buttons I would like it to set it to block. From reading around I understand that it is most likely due to the div not being registered yet by the time the script has run but I have put the script tags below all the content so I am confused as to why they have not been registered by the time the script runs.