I was sitting looking at a JS tutorial for show hide now I'm getting the error message "Cannot read property 'style' of null"

it's from here

https://www.w3schools.com/jsref/prop_style_display.asp

	<span class="hamIcon" onclick="showNav()">&#9776;</span>
function showNav() {
        var x = document.getElementById("mainControl");
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }

same with

function showNav() {
        document.getElementsByClassName('mainControl').style.display = 'block' ? 'none' : 'block';
    }

    You don't appear to have an element in the document with id [font=monospace]mainControl[/font], so [font=monospace]document.getElementById("mainControl")[/font] will return null. You don't have any elements with a class of [font=monospace]mainControl[/font] either, so [font=monospace]document.getElementsByClassName("mainControl")[/font] won't have anything to return either (and if it did, they'd be in an array, which won't have a [font=monospace]style[/font] even so.)

    Every time I hear about w3schools it's because of something wrong with it...

      oh, my main control

      <div class="mainControl">
          <div class="menuList">
              <div class="hamburger">
                  <ul class="spacing">
                      <li>Search</li>
                      <li>New local reviews</li>
                      <li>New local locations</li>
                      <li>Register</li>
                  </ul>
              </div>
          </div>
      </div><!--end main control!-->
      
        function showNav() {
                var mc = document.getElementById("mainControl");
                if (mc.style.display == 'block'){
                    mc.style.display = 'none';
                }    
        else{ mc.style.display = 'block'; } }
        <div class="mainControl" id="mainControl">
        

        That works

          Today I learned "&yen;" ... I guess I hadn't ever written about Japanese currency before ... hmm.

            dalecosp;11064315 wrote:

            Today I learned "¥" ... I guess I hadn't ever written about Japanese currency before ... hmm.

            Stop doing a me :p

              Write a Reply...