I'm currently working on a student project and I'm stuck with this problem for quite some time. I want to execute on one button different functions.
These are my buttons:
<button id="b1" type="button" onclick="a1Change(this)"><p id="a1">Answer 1</p></button>
<button id="b2" type="button" onclick="a1Change(this)"><p id="a2">Answer 2</p></button>
Now I have the JS function:
function a1Change(elem) {
if (elem.id == "b1") {
document.getElementById("a1").innerHTML = "Answer 1.1";
document.getElementById("a2").innerHTML = "Answer 1.2";
} else if (elem.id == "b2") {
document.getElementById("a1").innerHTML = "Answer 2.1";
document.getElementById("a2").innerHTML = "Answer 2.2";
}
If you click on the first button, it changes 'Answer 1' to 'Answer 1.1' and 'Answer 2' to 'Answer 1.2'. All good.
But now I want to click on button 1 again and it should change to 'Answer 1.1.1' and 'Answer 1.1.2'.
And here I'm stuck. I tried to change the onclick function after executing a1Change but I don't know how. Or maybe you have any better ideas how to solve this.