Why Array.from(classList)[0] = "className" not work

Viewed 28

Html code

<div class="container color-blue">
        <h3>HI I am a container</h3>
    </div>

js code

let div =document.getElementsByClassName('container')[0];
let a = div.classList;
// here is a is an object 
// so I use array.from(a) to turn in into array 
Array.from(a)[0] = "font";

"arrays are mutable and array can be chnaged" but it not replacing my container that is the first item of array with font

1 Answers

Array.from will create a new array from classList which is an instance of DOMTokenList.

If you must remove the first item do

let div =document.getElementsByClassName('container')[0];
let a = div.classList;
a.remove(a.item(0))

You can then just add the item you need

a.add("font");
Related