How to move a div in javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to move a div in javascript?

So i'm making a snake clone as a project and my current block is trying to move the div (snake) with the arrow keys. I've tried the event.keyCode method but that didn't work.

16th Oct 2018, 1:29 AM
Fbia34
Fbia34 - avatar
6 Answers
+ 3
document.onkeydown = checkKey; function checkKey(e) { e = e || window.event; if (e.keyCode == '38') { // up arrow } else if (e.keyCode == '40') { // down arrow } else if (e.keyCode == '37') { // left arrow } else if (e.keyCode == '39') { // right arrow } (got it from stackoverflow from a quick google search)
16th Oct 2018, 2:14 AM
Emperor Bob
Emperor Bob - avatar
+ 3
@Fbia34, I tip you to do it on jquery :P do you rly want to do it with JS only ?
16th Oct 2018, 7:57 AM
NoxFly
NoxFly - avatar
+ 1
Emperor Bob I looked on stack overflow and found nothing, maybe I should've look for more than 2 minutes
16th Oct 2018, 2:27 AM
Fbia34
Fbia34 - avatar
+ 1
just do a quick search on google that is "js detect arrow keypress"
16th Oct 2018, 3:45 AM
Emperor Bob
Emperor Bob - avatar
+ 1
NoxFly ドリアン I don't know jQuery so that's why
16th Oct 2018, 12:23 PM
Fbia34
Fbia34 - avatar
0
But thanks though
16th Oct 2018, 2:27 AM
Fbia34
Fbia34 - avatar