object - Very Basic setAttribute on JavaScript -


i'm beginner in javascript , can't understand why in simple code, appears in console color.setattribute not 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>
knowledge when declare variable color, create element object , can use method : setattribute.

thanks in advance, , sorry if question silly.

document.getelementsbyclassname("red") returns dom object array object. should write following.

var color=document.getelementsbyclassname("red")[0]; color.setattribute("class","blue"); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -