I want to trying adding class using javascript, I know there is a lot of topic about this in stackoverflow but some of them is too complicated for me to understand, let say I have this simple code
this is some topic that I already read:
addclass-to-getelementsbyclassname-array
add-css-class-using-document-getelementsbyclassname
add-class-using-getelementsbyclassname-javascript
this is my html
<p>
test 1
</p>
<h2 class="test-2">
test 2
</h2>
<h3 class="test-3">
test 2
</h3>
<button onClick="addClass">
click me
</button>
this is my css:
p{
color: red;
}
.test-2{
font-size: 2em;
color: blue;
}
.test-3{
font-size: 5em;
font-weight: bold;
}
and this is my js:
function addClass () {
var x = document.getElementsByClassName("test-3")[0];
return x[0].className += ' test-2';
}
where did I do it wrong? I'm quite confused since I'm new in javascript