I am trying to redesign a quiz template that only allows users to choose one answer before the other options are disabled. The user should be able to choose another answer (erasing the first) before submitting.
I can remove the highlighted color class if a new answer is chosen:
answer.classList.remove("correct");
How do I remove the element for the 'tick' icon:
let tickIconTag = '<div class="icon tick"><i class="fas fa-check"></i></div>';
from this parent element:
'<div class="option"><span>'+ multiple_choice_option[i] +'</span></div>'
when the option is set like this:
const option = option_list.querySelectorAll(".option");
for(i=0; i < option.length; i++)
{
option[i].setAttribute("onclick", "optionSelected(this);");
}//end for
and the tickIconTag is added like this:
answer.insertAdjacentHTML("beforeend",tickIconTag);
I have tried multiple ways but the main issue is that:
1.Using answer.insertAdjacentHTML("beforeend", blankelement) shifts tick inward not removed
2.Using tickIconTag.remove() gives the error "tickIconTag.remove is not a function"
3.Using answer.removeChild(tickIconTag) give the error "Parameter 1 is not of type node".
How can I set precisely the same element to blank and not shift it or remove it?
This is an image of what happens when I try to replace the element with a blank one. Green tick shifted but not removed
Pure CSS Solution
That's the exact behavior of a RadioGroupList. Click each circle in Figure I.
Figure I
Notice the
name
of each element is the sameA1
. This allows the group of radio buttons to have mutual exclusivity.With a
<label>
associated with each radio button, and some CSS we can recreate what is in the OP without JavaScript.<label>
s have a unique feature in which they can be associated with a form control by assigning thefor
attribute with the value of a form control's #id. Once associated a<label>
is insync with said form control so that if the<label>
was clicked, so to does the form control as long as both are on the same page. See Figure II.Figure II
In the CSS the following ruleset allowed the
<label>
to change whenever the checkbox was checked:Figure III
In Figure IV each
<label>
is associated to a hidden radio button. The user will only see and interact with the<label>
. Also, the example in Figure IV is responsive.Figure IV