So I'm trying to set .onclick functions that will change the color of whatever object I click to a large number of objects, lets just say they're <div>
objects.
var myArrayOfObjects = [...]; //get desired <div>s here
for(var i = 0; i < myArrayOfObjects.length; i++){
myArrayOfOjects[i].onclick = myFunction(i);
}
function myFunction(index){
//Make some edit to the <div> myArrayOfObjects[i] here
}
But setting object.onclick
equal to any function that takes any parameters makes the function run automatically and not when the particular object is clicked. If I try to access I any way other than passing it, it will always result in being myArrayOfObjects.length
. Is there a way to get the clicked <div>
to recognize exactly which object was clicked so it will know which one to change?