Skip to content Skip to sidebar Skip to footer

How To Close This Menu After Click

I am trying to customise This menu. This is the html markup :

    Solution 1:

    The id attribute should be unique in same document replace duplicate ones by class :

    <ulclass="topnav"id="myTopnav"><liclass="links"><ahref="#home">Home</a></li><liclass="links"><ahref="#news">News</a></li><liclass="links"><ahref="#contact">Contact</a></li><liclass="links"><ahref="#about">About</a></li><liclass="icon"><ahref="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.

    functionmyFunction() {
      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.topnavli {float: left;}
    
    ul.topnavlia {
      display: inline-block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px16px;
      text-decoration: none;
      transition: 0.3s;
      font-size: 17px;
    }
    
    ul.topnavlia:hover {background-color: #555;}
    
    ul.topnavli.icon {display: none;}
    
    @media screen and (max-width:1180px) {
      ul.topnavli:not(:first-child) {display: none;}
      ul.topnavli.icon {
        float: right;
        display: inline-block;
      }
    }
    
    @media screen and (max-width:680px) {
      ul.topnav.responsive {position: relative;}
      ul.topnav.responsiveli.icon {
        position: absolute;
        right: 0;
        top: 0;
      }
      ul.topnav.responsiveli {
        float: none;
        display: inline;
      }
      ul.topnav.responsivelia {
        display: block;
        text-align: left;
      }
    }
    <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ulclass="topnav"id="myTopnav"><liclass="links"><ahref="#home">Home</a></li><liclass="links"><ahref="#news">News</a></li><liclass="links"><ahref="#contact">Contact</a></li><liclass="links"><ahref="#about">About</a></li><liclass="icon"><ahref="javascript:void(0);"onclick="myFunction()">&#9776;</a></li></ul>

    Solution 2:

    Change links id to class, because ids are unique.

    <ulclass="topnav"id="myTopnav"><liclass="links"><ahref="#home">Home</a></li><liclass="links"><ahref="#news">News</a></li><liclass="links"><ahref="#contact">Contact</a></li><liclass="links"><ahref="#about">About</a></li><liclass="icon"><ahref="javascript:void(0);"onclick="myFunction()">&#9776;</a></li></ul>

    $(function($) {
        $('.links a').click(function() {
           $("#myTopnav").removeClass("topnav");
        });
    });
    

    You can even just select the li inside your myTopnav. For example:

    $('#myTopnav li').click(function() {
        $(this).parent().removeClass("topnav");
    });
    

Post a Comment for "How To Close This Menu After Click"