Can I select an element and change Classname like this?

422 views Asked by At
function name() {
    const select = document.querySelector("#id");
    select.classname = ("customclass")
}

I've written this code. But I'm unable to change the Classname.

1

There are 1 answers

2
Majed Badawi On BEST ANSWER

You need to change the className property:

const select = document. querySelector("#id"); 
select.className = "customclass";
console.log(select);
<div id="id"></div>

Another way would be adding it to the classList as suggested in the comments:

const select = document. querySelector("#id"); 
select.classList.add("customclass");
console.log(select);
<div id="id"></div>