When I click on the button, my goal is to display different text on click.
If the button says, Submit w/ Name & Email?, then all form fields should display.
If the button says, Submit Anonymously?, name and email fields should be removed.
Currently, only the name field disappears when the button is clicked, and it does return. I am trying to remove all fields with the class of pii
if the button is clicked.
How can I do that?
submitAnon.addEventListener("click", function myFunction() {
let btn = document.getElementById("submitAnon");
let submitAnon = 'Submit Anonymously?';
let submitEmail = 'Submit w/ Email?';
let pii = document.querySelector('.pii');
if (btn.innerHTML === submitAnon) {
btn.innerHTML = submitEmail;
pii.style.display = 'none';
} else {
btn.innerHTML = submitAnon;
pii.style.display = 'block';
}
});
<button id="submitAnon">Submit Anonymously?</button>
<form name="myForm" id="myForm">
<input type="text" class="pii" placeholder="your name">
<input type="email" class="pii" placeholder="your email">
<input type="text" class='non-pii' placeholder="city">
<textarea name="comments" id="comments" cols="30" rows="10" placeholder="your comments"></textarea>
</form>