I have div element "y" and a class of images called "terrain". The below code is intended such that whenever an element of the class is clicked, y and that element share top and left positions.
However, it doesn't seem to be working. What is the solution?
var lands = document.getElementsByClassName("terrain");
for(var i = 0; i < lands.length; i++){
lands[i].onclick = function(){
y.style.left = lands[i].offsetLeft;
y.style.top = lands[i].offsetTop;
};
}