Creating and invoking events in Javascript

I am working on a selection of my own adventure game, fully written in HTML and Javascript; I want to create most of my elements entirely in Javascript to create an awesome dynamic game! I'm having problems using events and event listeners. This is a secret game; after the player selects his character from the list, they can invite five guests to the party. One of the guests was killed, leaving you to solve the mystery with three suspects.

After you have selected the player, another button will appear with the inscription "Select this symbol!". When this button is pressed, the player creation user interface must be hidden and the new user interface will be visible. With the code, as now, the "StartGame" function is completely skipped. What am I doing wrong? Any help you can give would be seriously awesome and much appreciated!

btnPlayer = document.createElement('button');
btnPlayer.id = 'BTN_btnPlayer';
btnPlayer.type = 'button';
btnPlayer.addEventListener('click', welcomePlayer(), true);
btnPlayer.onclick = welcomePlayer();
btnPlayer.innerHTML = 'Select This Character!';
myDiv.appendChild(btnPlayer);

EDIT I changed the properties of the events in the button to look like this:

btnPlayer.addEventListener('click', welcomePlayer, true);
//btnPlayer.onclick = welcomePlayer;

, . . StartGame() . "charFirstNames []" "charLastNames []", . , ; , , , . , - , , ? , HTML.

function startGame(divName) {
myDiv = document.getElementById('story');

lblPlayer = document.createElement('label');
lblPlayer.id = 'LBL_Player';
lblPlayer.htmlFor = 'DDL_PlayerChar';
lblPlayer.innerHTML = 'Please select a character. ';
myDiv.appendChild(lblPlayer);

ddlPlayer = document.createElement('select');
ddlPlayer.id = 'DDL_PlayerChar';
myDiv.appendChild(ddlPlayer);

defOpt = document.createElement("option");
defOpt.value = 0;
defOpt.text = 'Select...';
ddlPlayer.appendChild(defOpt);

//Create and append the options
for (var i = 0; i < charFirstNames.length; i++) {
    var option = document.createElement("option");
    option.value = charFirstNames[i]+'_'+charLastNames[i];
    option.text = charFirstNames[i]+' '+charLastNames[i];
    ddlPlayer.appendChild(option);
}

document.getElementById('BTN_Start').hidden = true;

}

welcomePlayer() startgame(), "" .

+4
1

onclick welcomePlayer (. parens?).

btnPlayer.addEventListener('click', welcomePlayer(), true);
btnPlayer.onclick = welcomePlayer();

,

btnPlayer.addEventListener('click', welcomePlayer, true);
+5

Source: https://habr.com/ru/post/1627865/


All Articles