Answers for "add active class on open page using javascript"

1

Add active class onclick javascript

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            .active {
            padding: 5px;
            background-color: #20BF55;
            color: white;
            }
        </style>
    </head>
    <body>
        <div id="text" onclick="makeActive()">
            Click to make Active
        </div>
        <script>
            function makeActive() {
               var element = document.getElementById("text");
               element.classList.add("active");
               element.innerHTML="This is Active";
            }
        </script>
    </body>
</html>
Posted by: Guest on February-12-2021
0

how to add active class to current element javascript

<script>
  function func(){
   document.getElementById('div').classList.add('active');
  }
</script>

<button onclick='func()'>Click</button>
<div id="div">
  This element gets 'active' class.
</div>
Posted by: Guest on June-16-2021
0

add active class on open page using javascript

jQuery(function($) {
     var path = window.location.href; // because the 'href' property of the DOM element is the absolute path
     $('ul a').each(function() {
      if (this.href === path) {
       $(this).addClass('active');
      }
     });
    });
Posted by: Guest on October-03-2021

Code answers related to "add active class on open page using javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language