I would like to design a simple survey form. With only one question. I have decided to use an input checkbox. Although I thought about using a radio button for a long time. You can only choose one answer.
My goal
Is to have a clickable button across the entire width of the survey container. So without the selection check. If selected I would add a selected class to the selection.
My question
How can I implement what I want without losing the functionality of the selection? Actually, the theory is enough for me.
Here is my code:
function onlyOne(checkbox) {
const checkboxes = document.getElementsByName('check');
console.log('check clicked', checkbox)
checkboxes.forEach((item) => {
if (item !== checkbox) {
item.checked = false;
}
})
}
<div id="poll-id-1">
<h2>Poll</h2>
<p>Question?</p>
<div class="poll__div_vote-active">
<div>
<label>Yes</label>
<input type="checkbox" name="check" onclick="onlyOne(this)" value="0">
</div>
<div>
<label>No</label>
<input type="checkbox" name="check" onclick="onlyOne(this)" value="1">
</div>
</div>
<button class="poll__button_show-vote-results">Show Results</button>
<div class="poll__div_vote-result">
<div class="poll">
<div class="poll-results"></div>
</div>
</div>
</div>
Desgin what i expected
.a {
background: gray;
border:1px solid black;
margin-bottom:5px;
padding:10px;
cursor: pointer;
}
<div class="poll">
<div class="a">Yes</div>
<div class="a">No</div>
</div>
My try I found this snippet in an other question here on Stackoverflow. Now you would only have to remove the checkbox (the one with the hack). then i would be at my destination!?
label {
border:1px solid #ccc;
padding:10px;
margin:0 0 10px;
display:block;
}
label:hover {
background:#eee;
cursor:pointer;
}
<label><input type="checkbox" />Yes</label>
<label><input type="checkbox" />No</label>