2

I am using Javascript to try and loop through a list of divs with the same class name.I would like to change the box shadow and the border color on hover in JavaScript.So basically when my mouse is on the div the style changes then when my mouse is off the style reverts back. Here's what I've tried below:

var elements = document.getElementsByClassName("holla2");

for(i=0; i<elements.length; i++){
 elements.onmouseover = function(){
    element[i].backgroundColor = "black";
  }
}
Opp
  • 520
  • 1
  • 8
  • 21
  • 2
    Look very carefully at your logic. It doesn't do what you think it does. – Robert Harvey Feb 20 '19 at 05:42
  • 3
    whats the question so far? change "element[i]" to "elements[i]" – Ans Bilal Feb 20 '19 at 05:42
  • 3
    Have a look at [What do querySelectorAll and getElementsBy* methods return?](https://stackoverflow.com/q/10693845/218196) and [How to generate event handlers with loop in Javascript?](https://stackoverflow.com/q/6487366/218196) – Felix Kling Feb 20 '19 at 05:42
  • 1
    You can also look into `addEventListener` as well. There you will get `element[i]` as `this` – Rajesh Feb 20 '19 at 05:44

0 Answers0