简体   繁体   中英

Can I select an element and change Classname like this?

function name() {
    const select = document.querySelector("#id");
    select.classname = ("customclass")
}

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM