I have created a simple website for learning purposes with 2 buttons (2 awnsers on a question), let's say:
Button A and Button B
When you click on A it will display answer A inside a div on my website by innerHtml, and when you click on B it will display answer B or replace awnser A with awnser B inside the innerHtml and vice versa.
So far i thought i knew how it was working by using some scripts i found on Stackoverflow.
But now i want to create 2 more buttons like above to for question 2 with the same system as mentionent above
So a button C and D, but it keep's interfering with button A and B.
I hope someone here can help me create a page that can onclick display the awnser of question 2, underneith the awnser of question 1 without interfering.
Now i am using the code shown below fot question 1:
<button id="choiceA" class="Button" question-one="Awnser 1" onclick="questionOne(this)">
Q 1 choice A
</button>
<button id="ChoiceB" class="Button" question-one="Awnser 2" onclick="questionOne(this)">
Q1 choice B
</button>
And here's the script:
function questionOne(elem) {
var x = document.getElementById("questionOne");
var description = elem.getAttribute('question-one');
x.innerHTML = description;
var button = document.getElementsByClassName('js-button');
for (var i = 0; i < button.length; i++) {
button[i].classList.remove('active-button');
}
elem.classList.add('active-button');
}
And this code for question 2:
<button id="choiceA" class="Button" question-two="Awnser 1" onclick="questionTwo(this)">
Q 2 choice A
</button>
<button id="ChoiceB" class="Button" question-two="Awnser 2" onclick="questionTwo(this)">
Q 2 choice B
</button>
And the script:
function questionTwo(elem) {
var x = document.getElementById("questionTwo");
var description = elem.getAttribute('question-two');
x.innerHTML = description;
var button = document.getElementsByClassName('js-button');
for (var i = 0; i < button.length; i++) {
button[i].classList.remove('active-button');
}
elem.classList.add('active-button');
}
id="choiceA"andid="choiceB"in both sections. Those are repeated. I would say change those to classes, but your logic doesn't really use them so it's not clear if they are needed at all