JavaScript Onmouseover method dynamically allocating

I am trying to create a set of elements from CMS. I have reproduced the problem here with a set of rectangles that are being generated in Javascript. How can I dynamically add a "onmouseover" method to each one of them, that changes the colour of the block hovered?

for (i = 0; i < 5; i++) {
  var coloured_div = document.createElement("div");
  coloured_div.className = "col_div_class";
  coloured_div.id = "coloured_div" + i;
  document.getElementById("body").appendChild(coloured_div);

  coloured_div.addEventListener("mouseover", hoverCube);
}

function hoverCube(i) {
  document.getElementById("coloured_div" + i).style.backgroundColor = "orange";
}
.col_div_class {
  width: 250px;
  height: 250px;
  background-color: yellow;
  border: solid white 5px;
}
<body id="body">
</body>
Answer:1

As you're passing hoverCube as a callback to your event listener, it automatically gets passed an object which has information about the event (e). From this information, you can get the element which triggered the event by doing (e.target), which you can then set the style of:

for (var i = 0; i < 5; i++) {
  var coloured_div = document.createElement("div");
  coloured_div.className = "col_div_class";
  coloured_div.id = "coloured_div" + i;
  document.getElementById("body").appendChild(coloured_div);

  coloured_div.addEventListener("mouseover", hoverCube);
}

function hoverCube(e) {
  e.target.style.backgroundColor = "orange";
}
.col_div_class {
  width: 250px;
  height: 250px;
  background-color: yellow;
  border: solid white 5px;
}
<body id="body">
</body>
Answer:2

Append event listener to all elements. Select them with querySelectorAll. No ids needed.

for (i = 0; i < 5; i++) {
  var coloured_div = document.createElement("div");
  coloured_div.className = "col_div_class";
  document.getElementById("body").appendChild(coloured_div);
}

document.querySelectorAll('.col_div_class').forEach(function(el) {
  el.addEventListener('mouseover', function(e) {
    e.currentTarget.style.backgroundColor = "orange";
  });
});
.col_div_class {
  width: 250px;
  height: 250px;
  background-color: yellow;
  border: solid white 5px;
}
<body id="body"></body>
Answer:3

I want to change the arrow style function (at onChange) in this react component into an ordinary function. I'm a beginner and for me it is helpful to see both versions side by side since I have a hard ...

I want to change the arrow style function (at onChange) in this react component into an ordinary function. I'm a beginner and for me it is helpful to see both versions side by side since I have a hard ...

  1. arrow function into normal function

I am trying to add a static image as fallback for video background in html 5 but not getting the output can anyone help. <div class="background-wrap"> <video id="video-bg-elem" ...

I am trying to add a static image as fallback for video background in html 5 but not getting the output can anyone help. <div class="background-wrap"> <video id="video-bg-elem" ...

Scenario: I run TestCafé wrapped in code, using the API I have a test I want to parameterize, testing with different dynamic values. Problem Testcafé has no support for sending parameters to a ...

Scenario: I run TestCafé wrapped in code, using the API I have a test I want to parameterize, testing with different dynamic values. Problem Testcafé has no support for sending parameters to a ...

  1. inject parameters into service symfony
  2. inject parameter into service angular
  3. inject value into static variables
  4. inject value into annotation spring
  5. inject value into annotation
  6. inject value into bean
  7. inject value into enum
  8. inject value into constructor
  9. symfony inject parameter into controller
  10. java inject parameter into string

I am submitting a form with multiple file upload, all I want to differentiate the uploaded images according to the category which they uploaded <form action="upload.php" method="POST"> &...

I am submitting a form with multiple file upload, all I want to differentiate the uploaded images according to the category which they uploaded <form action="upload.php" method="POST"> &...