let btns = document.querySelectorAll("button");
function choosenOrder(chices1) {
let btnsPair = [chices1[0], chices1[1]];
btnsPair.forEach((choice) => {
choice.addEventListener("click", (c1) => {
let mychoice = c1.target.textContent;
return mychoice;
})
})
}
console.log(choosenOrder(btns));
//return undefined
I will be simple and direct...
I want to return the text value of the button i press using the listener function inside addEventListener. However this is not working as expected, and what ever i try it either returns undefined or not work at all, is it even possible to return a value from an event listener?
If it is not, what kinda of alternatives do I have?
Thanks in advance!
returnstatement. Only the inner callback has one. Also, you are looping before any button has pressed. All the collecting should be coded inside the event handler.chosenOrderdoesn'treturnanything. The function passed toaddEventListenerreturnsmyChoice. Ultimately the function has to be asynchronous (and streamable, if not observable) in order to "return" a value from an event listener. Sounds like you need to rethink your design.