• Dynamically changing class names in JavaScript

    I have a simple menu in HTML:

    <ul id="menu">
        <li><a id="admin" onclick="changeActive('admin');" class="active a red">Admin</a></li>
        <li><a id="dash" onclick="changeActive('dash');" class="a black">Dash</a></li>
        <li><a id="notifs" onclick="changeActive('notifs');" class="a black">Notifs</a></li>
    </ul>
    

    The idea is to dynamically change the active element active A .

    My current function:

    function changeActive(id)
        {
          document.getElementById(id).className += "active a red";
          var c = document.querySelectorAll("ul a");
          for (i in c)
          {
            fav = c[i].attributes[0];
            if(fav.value!=id)
              {
                console.log(fav.value);
                document.getElementById(fav.value).className += "a black";
              }
          }
        }
    

    Is there a more efficient solution? Thank!

    +4
    source share
    5 answers

    Please note that I deleted +=so that you do not add the same class every time.

    function changeActive(id) {
          var c = document.querySelectorAll("ul a");
          var i;
          for (i in c) {
            c[i].className = 'a black';
          }
          //update it at last
          document.getElementById(id).className = "active a red";
    }
    
    +5
    source

    Use the property classListand method add. removedelete class

      document.getElementById(id).classList.add("active a red");
    
    +3
    source

    navmenus li a .

    id, , onclick li ( href a) li , li. .active CSS a .

    function changeActive(option){
      var option = option;
      var li = document.getElementsByTagName("li");
      for(i=0;i<li.length;i++){
         li[i].classList.remove("active");
      }
      document.getElementById(option).classList.add("active");
    }
    ul{list-style:none}
    a{color:black;text-decoration:none}
    .active a{color:red}
    <ul id="menu">
        <li class="active" onclick="changeActive('admin')" id="admin"><a  href="#">Admin</a></li>
        <li onclick="changeActive('dash')" id="dash"><a href="#">Dash</a></li>
        <li onclick="changeActive('notifs')" id="notifs"><a href="#">Notifs</a></li>
    </ul>
    Hide result
    +3

    "" , . 3 .

    , , , , . , getElementById. addEventListener, , .

    querySelector, , , :

    function changeActive(evt) {
      // Get the current "active" element
      var active = document.querySelector('.active.a.red');
      // Make it not active
      active.className = 'a black';
      // Set the clicked on element to active
      this.className = 'active a red';
    }
    
    // Add listeners when the load event occurs
    window.onload = function(){
      Array.prototype.forEach.call(document.querySelectorAll('#menu a'), function(a) {
        a.addEventListener('click', changeActive,false);
        console.log(a.attributes[1].value)
      });
    };
    .active {
      color: red;
    }
    <ul id="menu">
        <li><a id="admin" class="active a red">Admin</a></li>
        <li><a id="dash" class="a black">Dash</a></li>
        <li><a id="notifs" class="a black">Notifs</a></li>
    </ul>
    Hide result

    , ... -, , . , id. , id.

    +2

    , ,
    HTML:

    <ul id="menu">
        <li><a class="active a red">Admin</a></li>
        <li><a class="a black">Dash</a></li>
        <li><a class="a black">Notifs</a></li>
    </ul>
    

    JQuery

    $( document ).ready(function() {
        $('#menu li a').on('click', function() {
            $('#menu li a').removeClass().addClass( "a black" );
            $(this).removeClass().addClass( "active a red" );
        });
    });
    

    HTML-, JS, , , . jQuery , CSS.

    . , jQuery.

    +1

    Source: https://habr.com/ru/post/1673178/


    All Articles