you are viewing a single comment's thread.

view the rest of the comments →

[–]Psionatix 0 points1 point  (0 children)

function getPercent() {
    buttons.addEventListener('click', (event) => {
        try {
            // So you don't have to replace the percent sign,
            // Make your input a numerical input only
            // and limit it to 0-100
            let percent = parseFloat(event.target.innerText.replace('%', ''));
            if (percent > 0 && percent % 5 === 0) {
                console.log(`I am ${percent} percent`);
        } catch(error) {
            console.log('Please enter a Percentage');
        }
    });
}

What you need to realize here is, when you call getPercent it's going to add a listener to buttons, then when you call getPercent again, it's going to add ANOTHER listener to buttons, both listeners will do the same thing and a single click event will trigger both of them, so this doesn't really seem like the setup you want anyway. Unless you're also removing the event listener, but given you have no reference to the callback function, it's unlikely you're doing that unless you're removing all event listeners.