What I´m trying to achieve is to have 1 question with 4 possible answers to choose from (radio buttons). When the user presses the button after the form, I want a paragraph to tell them more about the choice they made.
I´ve targeted the button, the paragraph and the radio buttons in my JavaScript. I´ve also managed to make the paragraph text appear whenever I choose the first radio button. However, I only want the text in the paragraph to appear if both one radio button has been chosen AND the button has been pressed. I want different content to appear in the paragraph depending on which radio button has been chosen.
Any tips or solutions would be greatly appreciated =)
Here is my code so far:
<form action="">
<h1>A friend invites you to a party. You...</h1>
<br />
<input id="red" type="radio" name="color" value="red">...bluntly tell your friend you have other priorities. <br/>
<input id="blue" type="radio" name="color" value="blue">...tell your friend you are finishing a coding assignment tonight. <br/>
<input id="yellow" type="radio" name="color" value="yellow">...hug your friend and start discussing the outfit. <br/>
<input id="green" type="radio" name="color" value="green">...thank your friend for inviting you, and tell her you look forward to it. <br/>
</form>
<button> Click me </button>
<p></p>
<script>
let btn = document.querySelector('button');
let para = document.querySelector('p');
let response = document.querySelector('input');
response.addEventListener('change', myColor);
function myColor() {
let choice = response.value;
if (choice === 'red') {
para.textContent = 'You´re so red!';
} else if (choice === 'blue') {
para.textContent = 'You´re so blue!';
} else if (choice === 'yellow') {
para.textContent = 'You´re so yellow!';
} else if (choice === 'green') {
para.textContent = 'You´re so green!';
}
}
</script>