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

206
Visualizações
How to get value from button after it has been clicked

I'm struggling with this assignment: Pin an event listener to the buttons. Create a function that gets called when one of the buttons is clicked. Check this with a console.log. Make sure the click event is passed to this function. Make sure you have access to the value of the button clicked in this function. Check this with console.log. The outcome you want to see in the console when you click is: Leopard / Lion / Elephant / Rhino or Buffalo.

fiveButtons = document.getElementsByClassName("big-five-button");
for (var i = 0; i < fiveButtons.length; i++) {
    fiveButtons[i].addEventListener("click", function () {
        
        Array.from(fiveButtons).forEach(function (nameButton) {
            console.log(nameButton.innerHTML);
        })
    });
}

This is what I wrote so far. When I'm clicking the button now, the outcome is the text from all the buttons. While I want the outcome to only be "Lion" after the button lion has been clicked.

<h1>The Big Five</h1>
    <ul class="big-five-list">
       <li class="big-five-list-item">
           <button class="big-five-button">Lion</button>
       </li> etc.
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

when creating an addEventListener you can use the event object to target the element clicked, like this:

fiveButtons[i].addEventListener("click", function (event) {
       console.log(event.target.innerHTML);
    });
about 4 years ago · Juan Pablo Isaza Relatório

0

You can change the button to include an onclick function like the below:

https://www.w3schools.com/jsref/event_onclick.asp

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction('Lion')">Lion</button>
<input type="text" value="" id="getValue">

<p id="demo"></p>

<script>
function myFunction(value) {
  document.getElementById("getValue").value = value;
}
</script>

</body>
</html>

The onclick function will then have a value inside the () for the function name. This will pass the value you want across to the function and it can be called whatever you want. The above snippet shows an example of how it can be used

about 4 years ago · Juan Pablo Isaza Relatório

0

EDITED

// Get all the buttons
const fiveButtons = document.getElementsByClassName("big-five-button");

// Iterate through the collection of buttons
// Here is let i = 0 instead of var i = 0, since var has functional scope and let has block scope
// If we used var i = 0 it would not work implicitly because i would exist in the scope of a function,
// and all the event handlers (for each button) would share the same value of i
for (let i = 0; i < fiveButtons.length; i++) {

  // For each button register event handler
  fiveButtons[i].addEventListener("click",  _ => {

    // When button is clicked this part of a code is being called
    // Because of javascript CLOSURE, it remembers the i value
    console.log(fiveButtons[i].innerHTML)

  });

}

If this is not understandable please read about closures in javascript.

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