Home > Software engineering >  How to get Id of Button that is clicked
How to get Id of Button that is clicked

Time:08-09

I'm doing the rock-paper-scissors game with the freeCodeCamp video and can't get the id of a button that is clicked and Insert it into the Page.

possibleChoices.forEach(button => button.addEventListener('click', (e){
   
        playerChoice = e.target.id
        playerChoiceDisplay.innerHTML = playerChoice
    

}))

This is how they did it in the video but somehow for me it always says that playerChoice is equal to null. Does this mean that the e.target.id doesn't work? I also wondered why they didn't put a function before the (e). Thank you for answering!

CodePudding user response:

Try this

possibleChoices.forEach(button => button.addEventListener('click', (e)=>{
   
        playerChoice = e.target.id
        playerChoiceDisplay.innerHTML = playerChoice
    

}))

you are writing e(){//..//..} which is invalid function, so you should write function e(){} or e=>{}

CodePudding user response:

    possibleChoices.forEach(button => button.addEventListener('click', (e)=>{
   
        playerChoice = e.target.id
        id = playerChoice.value
        playerChoiceDisplay.innerHTML = playerChoice
    

}))
  • Related