+ 3
How to add multiple elements to getElementsByClassName
Here is my code so far, I have got it working with a getElementById but am having trouble getting to have multiple elements added to it. Any help is much appreciated. https://code.sololearn.com/WeHf65idj0VK/#html
7 Respostas
+ 4
function pos(dx, dy){
if (!document.getElementById) return;
if ((x == 10 && dx == -1) ||
(x == 290 && dx == 1))
alert("can't move");
else
x += 5 * dx;
if ((y == 10 && dy == -1) ||
(y == 270 && dy == 1))
alert("can't move");
else
y += 5 * dy;
Added ifs to stop moving at edges. You could reset x & y instead.
+ 3
I've posted a useful explanation of how handle elements array-like returned by such functions (and suggested to use another one) in one of your previous question about the same subject, but you seems to not read it, and/or not getting so much attention to it, so I don't know why I would help you further ^^
https://www.sololearn.com/Discuss/1071147/?ref=app
+ 2
Im looking so that when you touch the borders it will restart the square at the starting position. @John Wells
+ 2
I don't know if i am correct but
getElementsByClassName returns an array so you can loop through it.
Or
The best way do it using jquery
+ 2
@visph I obviously read it dumb ass but I tried what they said to try with no luck so I tried again this time with posting my code so just chill out
+ 1
What exactly are you looking for?
Meanwhile, your hide and show after running game would be simpler to use class for the items.
<div id = "left_div" class=toggleMe></div>
<div id = "right_div" class=toggleMe></div>
$(".toggleMe").hide();
$(".toggleMe").show(3000);
Of course, every id that was listed in the hide/show needs the class name added.
0
getElementsByClassName will return an array. if you want to access an element of the array you need to use a number as an index: element[0], element[1], ... like this.