Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!