sy89
sy89

Reputation: 193

function not calling inside event listener

function main() {
    var dict = {};     
    document.getElementById("start").addEventListener('click',function(e) {
        e.preventDefault(); 
        if (document.getElementById("female").checked) { 
            dict["F"]=true;
        }

        if (document.getElementById("male").checked) {
            dict["M"]=true;
        }

        if (document.getElementById("gameA").checked){ 
             start(game.A);
        }
    });

So, when the start button is clicked, I want to create a dictionary of some values and then if the gameA box is checked, I want to start the game. However, the start function doesn't seem to work when it's inside the eventListener. The start function should display text on the screen and then play audio. The text shows up, but the audio doesn't play.

If I put the start function outside the event listener, but still inside the main function using:

document.getElementById("gameA").addEventListener('click',function () {
    start(game.A);
}); 

Then, the audio works fine. However, I don't know how to allocate the dictionary because if I put the lines that allocate it outside the event listener, it doesn't work for some reason.

Does anyone have any tips?

Upvotes: 0

Views: 145

Answers (1)

Alpesh Jikadra
Alpesh Jikadra

Reputation: 1722

Why don't you try something like

function main() {
    var dict = {};     

        event.preventDefault(); 
        if (document.getElementById("female").checked) { 
            dict["F"]=true;
        }

        if (document.getElementById("male").checked) {
            dict["M"]=true;
        }

        if (document.getElementById("gameA").checked){ 
             start(game.A);
        }
}

And HTML like

<input type="button" value="StartButton" onclick="main();"/>

Upvotes: 1

Related Questions