Q:

How to close this menu after click

I am trying to customise This menu.
This is the html markup :
<ul class="topnav" id="myTopnav">
  <li id="links"><a href="#home">Home</a></li>
  <li id="links"><a href="#news">News</a></li>
  <li id="links"><a href="#contact">Contact</a></li>
  <li id="links"><a href="#about">About</a></li>
  <li class="icon">
    <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
  </li>
</ul>

CSS :
ul.topnav {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

ul.topnav li {float: left;}

ul.topnav li a {
    display: inline-block;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

ul.topnav li a:hover {background-color: #555;}

ul.topnav li.icon {display: none;}

@media screen and (max-width:1180px) {
  ul.topnav li:not(:first-child) {display: none;}
  ul.topnav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.topnav.responsive {position: relative;}
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}

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

I am trying to add this to make the menu close after user clicks on any of the list items-
$(document).ready(function($) {
    $('#links').click(function() {
       $("#myTopnav").removeClass("topnav");
    });
});

But this does not seem to work.
Any idea how to fix this?

A:

The id attribute should be unique in same document replace duplicate ones by class :
<ul class="topnav" id="myTopnav">
    <li class="links"><a href="#home">Home</a></li>
    <li class="links"><a href="#news">News</a></li>
    <li class="links"><a href="#contact">Contact</a></li>
    <li class="links"><a href="#about">About</a></li>
    <li class="icon">
      <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
    </li>
</ul>

Use class selector . in your JS code :
$(document).ready(function($) {
    $('.links').click(function() {
       $("#myTopnav").removeClass("responsive");
    });
});

NOTE : You have to remove responsive class instead of topnav.
Hope this helps.

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

$(document).ready(function($) {
  $('.links').click(function() {
    $("#myTopnav").removeClass("responsive");
  });
});
ul.topnav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

ul.topnav li {float: left;}

ul.topnav li a {
  display: inline-block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  transition: 0.3s;
  font-size: 17px;
}

ul.topnav li a:hover {background-color: #555;}

ul.topnav li.icon {display: none;}

@media screen and (max-width:1180px) {
  ul.topnav li:not(:first-child) {display: none;}
  ul.topnav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.topnav.responsive {position: relative;}
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="topnav" id="myTopnav">
  <li class="links"><a href="#home">Home</a></li>
  <li class="links"><a href="#news">News</a></li>
  <li class="links"><a href="#contact">Contact</a></li>
  <li class="links"><a href="#about">About</a></li>
  <li class="icon">
    <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
  </li>
</ul>

