fairly new to coding and I'm building out a connect four game. The code I pasted allows me to drag and drop the red & yellow tokens to the cells of the top row of the game board. This works, but before I move on to the next step I would like to know if there is a less "verbose" way that I could have gone about this? A more professional approach?
var dragYellow = document.getElementById("yellowToken1");
var dragRed = document.getElementById("redToken1");
var dropLoc10 = document.getElementById("10");
var dropLoc11 = document.getElementById("11");
var dropLoc12 = document.getElementById("12");
var dropLoc13 = document.getElementById("13");
var dropLoc14 = document.getElementById("14");
var dropLoc15 = document.getElementById("15");
var dropLoc16 = document.getElementById("16");
dragYellow.ondragstart = function(evt) {
evt.dataTransfer.setData("key", "yellowToken1");
}
dragRed.ondragstart = function(evt) {
evt.dataTransfer.setData("key", "redToken1");
}
dropLoc10.ondragover = function(evt) {
evt.preventDefault();
}
dropLoc11.ondragover = function(evt) {
evt.preventDefault();
}
dropLoc12.ondragover = function(evt) {
evt.preventDefault();
}
dropLoc13.ondragover = function(evt) {
evt.preventDefault();
}
dropLoc14.ondragover = function(evt) {
evt.preventDefault();
}
dropLoc15.ondragover = function(evt) {
evt.preventDefault();
}
dropLoc16.ondragover = function(evt) {
evt.preventDefault();
}
dropLoc10.ondrop = function(evt) {
evt.preventDefault();
var dropItem = evt.dataTransfer.getData("key");
var myElement = document.getElementById(dropItem);
var myNewElement = document.createElement("img");
myNewElement.src = myElement.src;
dropLoc10.appendChild(myNewElement);
}
dropLoc11.ondrop = function(evt) {
evt.preventDefault();
var dropItem = evt.dataTransfer.getData("key");
var myElement = document.getElementById(dropItem);
var myNewElement = document.createElement("img");
myNewElement.src = myElement.src;
dropLoc11.appendChild(myNewElement);
}
dropLoc12.ondrop = function(evt) {
evt.preventDefault();
var dropItem = evt.dataTransfer.getData("key");
var myElement = document.getElementById(dropItem);
var myNewElement = document.createElement("img");
myNewElement.src = myElement.src;
dropLoc12.appendChild(myNewElement);
}
dropLoc13.ondrop = function(evt) {
evt.preventDefault();
var dropItem = evt.dataTransfer.getData("key");
var myElement = document.getElementById(dropItem);
var myNewElement = document.createElement("img");
myNewElement.src = myElement.src;
dropLoc13.appendChild(myNewElement);
}
dropLoc14.ondrop = function(evt) {
evt.preventDefault();
var dropItem = evt.dataTransfer.getData("key");
var myElement = document.getElementById(dropItem);
var myNewElement = document.createElement("img");
myNewElement.src = myElement.src;
dropLoc14.appendChild(myNewElement);
}
dropLoc15.ondrop = function(evt) {
evt.preventDefault();
var dropItem = evt.dataTransfer.getData("key");
var myElement = document.getElementById(dropItem);
var myNewElement = document.createElement("img");
myNewElement.src = myElement.src;
dropLoc15.appendChild(myNewElement);
}
dropLoc16.ondrop = function(evt) {
evt.preventDefault();
var dropItem = evt.dataTransfer.getData("key");
var myElement = document.getElementById(dropItem);
var myNewElement = document.createElement("img");
myNewElement.src = myElement.src;
dropLoc16.appendChild(myNewElement);
}