dimanche 8 février 2015

Multiple choice Javascript - Looking for a smart way to make it work

I am trying to make a multiplechoice game with all the text inside variables :


For exemple the HTML code is :



<p id ="story"></p>

<div id="hide1"><input type="radio" name="sex" id="Button1" class="Button"><p id="choice1">Choice1</p></div>

<div id="hide2"><input type="radio" name="sex" id="Button2" class="Button"><p id="choice2">Choice2</p></div>

<button type="button" id="button" onclick="myFunction()">Continue</button>


The Javascript code is :



var Choix = [
"TEST1",
"TEST2",
"TEST3",
"TEST4",
"TEST5",
];

document.getElementById("story").innerHTML = Choix[0];

function myFunction()
{
if (document.getElementById('Button1').checked)
{
document.getElementById("story").innerHTML = Choix[1];
document.getElementById('choice1').innerHTML = "Choice3";
document.getElementById('choice2').innerHTML = "Choice4";
}
else if(document.getElementById('Button2').checked)
{
document.getElementById("story").innerHTML = Choix[2];
}
else if(document.getElementById('Button3').checked)
{
document.getElementById("story").innerHTML = Choix[3];
}
}


The main issue actually is that my radio buttons are not changing when I display a different Var... it always lead me to the same part of the function. I made a lot of test but the few Ideas I found sound really bad to me (it includes tons of coding) and I am sure there is a smart way to keep it simple. But I can't find one. :/


I would like to keep the button which call a function.


Maybe a switch will be better than an if / else if ?


I am a beginner so I hope my question was clear enough :/


Thank you


Aucun commentaire:

Enregistrer un commentaire