samedi 8 décembre 2018

How do I get the id of a clicked parent element in a card?

I'm trying to make certain "cards" that display next to each other, and when the card is clicked, I want to get the id of the card. However, my function gets the id of the child elements when I click it, which is undefined, rather than of the parent card, unless I click on the border of the card div. How do I make sure it gets the id of the card specifically?

Here is the HTML:

<div id="parentCard" onclick="getID(event)">
  <div id="child1">I'm a child</div>
  <div id="text">
    <p>Text 1</p>
    <p>Text 2</p>
    <p>Text 3</p>
  </div>
</div>

Javascript:

function getID(event){
  console.log(event.target.id);
}




Aucun commentaire:

Enregistrer un commentaire