0

I want the user to be able to choose whether they want the received ace card to be worth 1 or 11. I therefore prompt them with two buttons (#aceOne and #aceEleven) but I want to wait until they set the value themselves by pressing one before continuing the game.

The pressed buttons invoke the function aceWorth(ace)(This function also does other things) - passing the parameter 1 or 11 depending on button press, the function sets its value.

So - how do I force halt the code until the user presses one of the buttons - and then run the function (aceworth) to set the value of the card?

document.getElementById("aceOne").addEventListener("click", aceWorth(1))
document.getElementById("aceEleven").addEventListener("click", aceWorth(11))

function aceWorth(ace){
    window.cardValue = ace}

1 Answer 1

2

Using the below line while passing the return value of aceWorth(1) as parmeter instead of the function is not how you can bind events.

You are calling aceWorth() and binding it's return value as argument. Try to use the arrow Syntax () => {doSomething();}

.addEventListener("click", aceWorth(1))

You can do something like this:

function aceWorth(ace) {
    window.cardValue = ace;
    console.log(ace);
}
    
aceOne.addEventListener("click", () => aceWorth(1));
aceEleven.addEventListener("click", () => aceWorth(11));
<button id="aceOne">1</button>
<button id="aceEleven">11</button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.