1

I have some problems with chaining some events.

I have added an eventListener on several links which on mouseover have to display a drop-down menu. This works as expected, however I also want some triangles to drop down with the menu.

So far, I have tried to match the array indexes of the link elements with the array indexes of the triangles. This works in a strange way. Basically, the menu will drop down but the triangles appear only after I hover the mouse to the second link, like in the image below:

Triangel 1 and 2 appear after moving the mouse to the second link

This behavior continues as I hover the mouse over the other elements. The triangles are displayed as they should.

I know they will remain there because I haven't added the mouseleave event for them.

Here is the code. I'm guessing there is a problem with matching the right array indexes?

let mainCateg = document.querySelectorAll(".main-categ");
  let dropDown = document.querySelector(".drop-down");
  let triangle = document.querySelectorAll('.triangle');

  
  loadEventListeners();
  
  function loadEventListeners() {
    for(i = 0; i < mainCateg.length; i++){
    mainCateg[i].addEventListener('mouseover', drop);
  }
    dropDown.addEventListener('mouseleave', leave );

}

    function drop(e){
  
    for(i = 0; i < mainCateg.length; i++){
      if(mainCateg[i] === e.target){
        triangle[i].style.display = 'block';
        
      }
    }
    dropDown.className ='down';
  }

Any help is much appreciated!

1 Answers1

0

Change mainCateg[i] === e.target to mainCateg[i] === e.currentTarget

You can check here the difference between target and currentTarget

quirimmo
  • 9,800
  • 3
  • 30
  • 45