Mobile Nav Javascript Not Working

Viewed 89

I'm a bit of a beginner here, and doing exercises on FreeCodeCamp. While working on the Portfolio Site project, I tried adding a Javascript responsive mobile nav to the site from some code at ws3schools.

The nav is showing up fine, but clicking it does nothing. Here's my CodePen, any ideas what I did that's preventing it from popping up the nav links on click? https://codepen.io/colum1225/pen/xLwMJG

Here's the nav bar code:

<!-- HTML -->

<div class="topnav" id="myTopnav">
  <h3>Brandon Ray</h3>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#portfolio">Portfolio</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<!-- CSS -->

.topnav {
  background-color: #2C8597;
  overflow: hidden;
  width: 100%;
  position: fixed;
  z-index: 9;
}

.topnav h3 {
  float: left;
  color: #f2f2f2;
  font-family: sans-serif;
  font-size: 1.5em;
  padding-left: 30px;
  letter-spacing: .04em;
  font-weight: 500;
}

.topnav a {
  float: right;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 27px 30px 27px 30px;
  text-decoration: none;
  font-size: 1.25em;
  font-family: sans-serif;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
  transition: .5s;
}

.topnav a:active {
  background-color: #ddd;
  color: black;
}

.topnav .icon {
  display: none;
  font-size: 1.3em;
}


@media screen and (max-width: 640px) {
  .topnav a {display: none;}
  .topnav .icon {
    float: right;
    display: block;
    color: white;
    margin-right: 30px;
  }
}

@media screen and (max-width: 640px) {
  topnav.responsive {position: relative;}
  topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }

  topnav.responsive a {
    float: none;
    display: block;
    text-align: right;
  }
}

<!-- JS -->

function myFunction() {
  var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }

}

2 Answers
Related