Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding class to div element using javascript

im really new to js, and im currently making a webiste. Because i have a repeated html code for the header and footer of the wepage, i used php to call the html file rather than repeating the same code. On the other hand,i wanted the current active page to have a different color, therefor i am using javascript to get the name of the class and add the class active to make a exeption in css. getting the id seems to be working, but i cant find the class active being add. why is this?

<p class="activepage" id="felt">aegearg<p>    
<script>
      var element = document.getElementsByClassName("activepage")[0];
      var id = element.id;
      getElementsByClassName(id).classList.add("active");
    </script>
like image 631
KAZ Avatar asked Nov 22 '25 18:11

KAZ


1 Answers

you can use this code:

document.getElementById("IdElement").classList.add("ClassNameAdd");
like image 167
Diar J R Avatar answered Nov 24 '25 07:11

Diar J R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!