'classList' property returning null when element is present?

Viewed 53

I'm fairly new to JS so I'm sorry if this has been answered.

I've siphoned through most answers and they didn't specifically fall in line with mine.

I'm not understanding why the element cant is called upon when it's there.

I don't think it seems to be anything with the CSS but I'm not completely sure.

const navSlide = () => {
  const burger = document.querySelector('.burger');
  const nav = document.querySelector('nav-links');


  burger.addEventListener('click', () => {

    nav.classList.toggle('nav-active');

  });

}


navSlide();
  display: flex;
  justify-content:space-around;
  align-items: center;
  min-height: 8vh;
  background-color: var(--mainBg);
}

.nav-links li{
  list-style: none;
 }


.nav-links{
  display: flex;
  justify-content: space-evenly;
  width: 20%;
  font-size: 20px;
  
  }
 
 .nav-links a{
  letter-spacing: 2px;
  color:var(--mainText);
} 

.burger{
  display: none;
  cursor: pointer;
}


.burger div{
  width: 25px;
  height: 4px;
  background-color: var(--mainText);
  margin:3px;
  padding: 0;

}


.showacase-hero{
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),url("img/D1.jpg");
  height: 600px;
  background-position:center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;

}

.hero-container{
  text-align: center;
  position: absolute;
  top: 50%;
  left: 28%;
  color: var(--mainBanner);
  font-size: 20px;
}   

/* Media Queries*/

  @media screen and (max-width:1024px){
  .nav-links{
    width: 60%;
  }
} 

@media screen and (max-width:728px){
    body{
  overflow-x: hidden;
   }
  .nav-links{
    position: fixed;
    right: 0px;
    height: 92vh;
    top: 8vh;
    background-color:var(--mainBg);
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50%;
    transform: translateX(100%);
    transition: transform 0.5s ease-in;
  }
  .nav-links li{
    opacity: 0;
  }
  .burger{
    display: block;
    margin-left: 28em;
  }

  .nav-active{
    transform: translateX(0%);
  }

}

@keyframes navLinkFade{
  from{
    opacity: 0;
    transform: translateX(50px);

  }
  to{
    opacity: 1;
    transform: translateX(0px);
  }

}

@media screen and(max-width:566px){
}
<body>
  <nav>
        <ul class="nav-links"> 
          
          <li><a href="index.html ">Home</a></li>
              <li><a href="about.html ">About</a></li>
              <li><a href="services.html ">Services</a></li>
              <li><a href="contact">Contact</a></li>
          </ul>

          <div class="burger">
              <div class="line1"></div>
              <div class="line2"></div>
              <div class="line3"></div>
        </div>

  </nav>

  <div class="showacase-hero">
      <div class="hero-container">
      <h1>Yade's Pet</h1>
      <span class="banner"><p>Petcare Specialist/Dog Walker</p></span>
      <form action="https://www.w3docs.com/">
           <button class="btn" type="submit">Let's Take a Stroll</button>
      </form>
      </div>
  </div>
  <script type="text/javascript" src="app2.js"></script>
</body>
</html>
    

1 Answers

nav-links is a class, not a tag, so add a dot before the class name:

document.querySelector( '.nav-links' );

PS: It's a bad practice to use variables const navSlide = () => { instead of a clear function declarations:

function  navSlide () {
   ...
}
Related