I've the following HTML:
<li class="treeview" id="account_management">
So I want to target the element with id
"account_management" and change it's class from "treeview" to "treeview active", so that I can then style my tree menu accordingly.
I've the following HTML:
<li class="treeview" id="account_management">
So I want to target the element with id
"account_management" and change it's class from "treeview" to "treeview active", so that I can then style my tree menu accordingly.
You can use jQuery addClass()
to add the class active
:
$("#account_management").addClass("active");
Or you can use Element.classList DOM API's add()
method like:
document.getElementById("account_management").classList.add("active");
classList browser support @caniuse
for older versions, see this answer.
In pure JavaScript you could do:
var d = document.getElementById("account_management");
d.className = d.className + " active";
If you already use jQuery in your project, I would recommend to make use of it like in T J's answer.