I'm very beginner in JavaScript and I can't understand why in this simple code, appears in the console color.setAttribute is not a function
<style>
.red {color:red;}
.blue { color: blue;}
</style>
</head>
<body>
<p class="red">Hello World</p>
<script>
var color = document.getElementsByClassName("red");
color.setAttribute("class","blue");
</script>
Thanks in advance, and sorry if my question is to silly.
document.getElementsByClassName("red") returns a dom object which is array like object. so you should write the following.
var color=document.getElementsByClassName("red")[0];
color.setAttribute("class","blue");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With