简体   繁体   中英

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.

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

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

Try this solution!

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

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);
    });

The function you pass to addEventListener gives an event argument:

fiveButtons = document.getElementsByClassName("big-five-button");
for (var i = 0; i < fiveButtons.length; i++) {
    fiveButtons[i].addEventListener("click", function (event) { // use the first argument
        console.log('element value:', event.target.value); // log the 'value' of the event target;
        // I suspect you want the innerHTML or innerText
        console.log('element innerText:', event.target.innerText);
    });
}

You can then get the required information from the DOM node in event.target

You don't need the Array.from inside the for loop. You can just do that:

fiveButtons = document.getElementsByClassName("big-five-button");
for (let i = 0; i < fiveButtons.length; i++) {
    fiveButtons[i].addEventListener("click", function () {
        console.log(fiveButtons[i].innerText);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM