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

97
Visualizações
Find out which div was clicked and pass it to the click function handler?

I'm adding a list of child divs (cards) by looping through an array. I want to create an onclick event listener for each child div (card) of parent div (board).

$.each(cardArray, function(i,value){

if(i<9){
  var tCard = $('<div class="cardContainer" title="'+cardArray[i]['name']+'" data-id="'+i+'">'+cardArray[i]['damage']+'</div>')
  $("#area_myCards").append(tCard)
}

});
$("#area_myCards > .cardContainer").on('click',cardClick())
    <div id="area_myCards"></div>

but I'm unsure how to find out which child div was clicked and pass it to cardClick()? I'm also going to be using the child's data-id within cardClick() and I'd like to know how to get this- should I pass it to the function in the on click listener somehow, or do it within the function?

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

0

Something like this?

const cardArray = [
  {name: 'A', damage: 'A-D'},
  {name: 'B', damage: 'B-D'},
  {name: 'C', damage: 'C-D'},
  {name: 'D', damage: 'D-D'},
  {name: 'E', damage: 'E-D'},
  {name: 'F', damage: 'F-D'},
  {name: 'G', damage: 'G-D'},
  {name: 'H', damage: 'H-D'},
  {name: 'I', damage: 'I-D'}
];
$.each(cardArray, function(i,value){

if(i<9){
  var tCard = $('<div class="cardContainer" title="'+cardArray[i]['name']+'" data-id="'+i+'">'+cardArray[i]['damage']+'</div>')
  $("#area_myCards").append(tCard)
}

});
$("#area_myCards > .cardContainer").on('click',e => cardClick(e.target))

const cardClick = (div) => {
  $(div).toggleClass('clicked');
};
.cardContainer { margin: 2px; padding: 10px; border: 1px solid #eee; }
.cardContainer.clicked { background-color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="area_myCards"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming that cardClick() is defined somewhere else, first of all, you would call it with $("#area_myCards > .cardContainer").on('click',cardClick) (without the parenthesis at the end).

That function would get passed an event as its first argument (usually called e), and the event has a property called currentTarget that can be used to see which instance has been clicked.

From MDN:

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.target, which identifies the element on which the event occurred and which may be its descendant.

So the cardClick function would be something like:

const cardClick = (e) => {
    const targetInstance = e.currentTarget;
    // Do stuff with your specific card...
    // To access data-id: targetInstance.getAttribute('data-id') 
    // or targetInstance.dataset.id
}
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