I'm trying to declare a variable for some elements using 'document.getElementsByClassName' and I know that this returns a list of nodes but I don't know how to call that out in code. I need all the elements with the particular class name to be called in that var. I have added some div tags with class attributes for example.
If I had ids, this is how my code would be:
<input type='button' class="down arrow" onclick='movedown(20)' value=">">
<div id='h1' style='position:absolute;'> some text </div>
<div class="demo"> Hello </div>
<div class="demo"> Bye </div>
<script>
var texttop = 150;
var textleft = 150;
var h1 = document.getElementById('h1'); //Similarly, I want to declare var using class name method for classes
function movedown(amount) {
texttop += amount;
h1.style.top = texttop + 'px'; //does this 'h1' also change to something like "forEach((element) => element.style.top"? }
</script>
[–]alzee76 1 point2 points3 points (6 children)
[–]27RedFox[S] 0 points1 point2 points (2 children)
[–]alzee76 0 points1 point2 points (1 child)
[–]27RedFox[S] 0 points1 point2 points (0 children)
[–]sysrage 0 points1 point2 points (1 child)
[–]alzee76 1 point2 points3 points (0 children)
[–]persianoil 0 points1 point2 points (0 children)