Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

74
Visualizações
Array from filter click

I have a list of elements, I want to get on click only the one that I clicked on.I tried to do it this way

const menuItems = document.querySelectorAll(".Account-navigation-item-link");

menuItems.forEach((item) => {
  item.addEventListener("click", () => {
    let its = Array.from(menuItems)
      .filter((i) => i.click)
console.log(its)




  });
});
  <ul class="Account-navigation">
   
  <a
        href="#"
        class="Account-navigation-item-link Account-navigation-item-link-active"
        >Мои данные</a
      >
  <a
        href="#"
        class="Account-navigation-item-link"
        >Схемы лечения</a
      >
   
   
      <a
        href="/logout"
        class="Account-navigation-item-link"
        >Выйти</a
   
  </ul>

But now I get all elements

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use event.target or event.currentTarget to get the element that is clicked. The difference is that event.currentTarget will always return the element which we have bound the click event listener to, while event.target may refer to descendants inside the clicked element.

In your case, since the <a> tags have no descendants, event.target or event.currentTarget will always yield the same element, and it shouldn't matter which one you use.

const menuItems = document.querySelectorAll(".Account-navigation-item-link");

menuItems.forEach((item) => {
  item.addEventListener("click", (e) => {
    console.log(e.currentTarget);
  });
});
<ul class="Account-navigation">
   
  <a
        href="#"
        class="Account-navigation-item-link Account-navigation-item-link-active"
        >Мои данные</a
      >
  <a
        href="#"
        class="Account-navigation-item-link"
        >Схемы лечения</a
      >
   
   
      <a
        href="/logout"
        class="Account-navigation-item-link"
        >Выйти</a
   
  </ul>

about 4 years ago · Juan Pablo Isaza Relatório

0

const menuItems = document.querySelectorAll(".Account-navigation-item-link");
menuItems.forEach((item) => {
  item.addEventListener("click", () => {  
      let its = Array.from(menuItems).filter(function(e) {
            return event.currentTarget == e;
      })
      console.log(its);
    
  });
});

You should try this.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda