This function isn't specific but generic. With the code below you can assign this function to all buttons if you like. I'd advice you to store your different texts in an array
example
<button onclick="exploreFunction(this, functionNext)" >Explore</button>
function exploreFunction(button, functionToContinue) {
var person = prompt("What is your name", "");
if (person != null) {
document.getElementById("story1").innerHTML =
"-You wake up lying on the ground. You feel a large deposit of energy inside you.";
button.onclick = functionToContinue;
}
}
function functionNext()
{
//Example code
document.getElementById("story1").innerHTML =
"-The tooth fairy really came through and you feel enchanted.";
//Other code come here
this.onclick = [new function]; //add your own function name here do not add () because it will get executed when you do, and you want it executed when the user clicks the button.
}
What does the code above do:
- Adds the
exploreFunction
to the button (refer to itself by adding this to the arguments).
- Supply the next function as argument
- function
exploreFunction
get executed , name is prompted and innerHTML of story1 is updated.
- button refers to the input element. Assign a new onclick handler
functionNext
- Next time the user clicks on the button
functionNext
gets executed.
functionNext
assigns another onclick handler (to be made by you).
Are you catching the drift?
Since you have the button object in exploreFunction
and also in the subsequent functions the this
variable refers to the button object, you can adjust other properties of this button. Such as the value.
example in the functionNext
:
this.value = "Click me to advance";