Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda