Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
responsive nav bar add class to divelement

i took this code from w3school where create a responsive nav bar. the nav bar add icon, add functionality for icon and change menu display. in js code, nav is assign to div element and nav has 2 case, if has class topnav or else (dont have class topnav). in html, nav has class top nav, so i dont understand what else case is for. else case is needed because when i remove else case functionality also change. please someone explain

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

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

/* .topnav a.active {
  background-color: #04aa6d;
  color: white;
} */

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  /* only display home */
  .topnav a:not(:first-child) {
    display: none;
  }
  /* display icon dropdown  */
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
    />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="topnav" id="myTopnav">
      <a href="#home">Home</a>
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
      <a href="javascript:void(0);" class="icon" onclick="myFunction()">
        <i class="fa fa-bars"></i>
      </a>
    </div>

    <script>
      function myFunction() {
        //objecthtmldivelement
        var nav = document.getElementById("myTopnav");
        // alert(nav);
        if (nav.className === "topnav") {
          //class responsive position icon to top right
          //and display a as block text float left
          nav.className += " responsive";
        } else {
          nav.className = "topnav";
        }
      }
    </script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

this function is for /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */

else part is for removing the "responsive" class so that it remain topnav like default it was

about 4 years ago · Juan Pablo Isaza Report

0

nav.className === "topnav" is a exact match statement, so if you click the icon, the nav.className become 'topnav responsive', which is not exact match to 'topnav'(in other words, 'topnav' === 'topnav responsive' is false), this is the functionality that the else block want to handle.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!