0

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>
Max Pattern
  • 1,430
  • 7
  • 18
  • Could you provide the CSS for the HTML you shared? – Dave111 Jan 17 '23 at 09:36
  • @Dave111 So far I haven't written any CSS because I didn't know how? That's why I'm asking here. My guess is that I have to hide the input field and work with the label. I have added a codesnippet how it should look like. But as I said I don't know how to implement it. – Max Pattern Jan 17 '23 at 09:48

2 Answers2

1

[type="radio"] {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

label {
  color: white;
  background: grey;
  display: block;
  cursor: pointer;
  padding: 1em;
  
  border:1px solid black;
  margin-bottom:5px;
  padding:10px;
}

[type="radio"]:checked+label {
  background: blue;
}

[type="radio"]:hover+label {
  background: #eee;
  color: #333;
}
<input type="radio" name="check" value="0" id="yes">
<label for="yes">Yes</label>

<input type="radio" name="check" value="1" id="no">
<label for="no">No</label>

To use checkboxes instead of radio inputs for some reason

[type="checkbox"] {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

label {
  color: white;
  background: grey;
  display: block;
  cursor: pointer;
  padding: 1em;
  
  border:1px solid black;
  margin-bottom:5px;
  padding:10px;
}

[type="checkbox"]:checked+label {
  background: blue;
}

[type="checkbox"]:hover+label {
  background: #eee;
  color: #333;
}
<input type="checkbox" name="check" value="0" id="yes">
<label for="yes">Yes</label>

<input type="checkbox" name="check" value="1" id="no">
<label for="no">No</label>
ksav
  • 20,015
  • 6
  • 46
  • 66
  • Did you know it is possible to use your solution for checkbox? Take a view on my other question: https://stackoverflow.com/questions/75145338/change-parent-label-background-when-checkbox-is-checked/75145374#75145374 – Max Pattern Jan 17 '23 at 11:37
  • Yes it's possible. But only if `label` is an adjacent sibling of `input`. – ksav Jan 17 '23 at 11:39
  • I'm trying, but unfortunately I can't get it to work. Can you please help me here? Then my question would be 100% answered. – Max Pattern Jan 17 '23 at 11:41
  • Instead of all that CSS for `[type="radio"]`, you could just have `display: none`. – Rickard Elimää Jan 17 '23 at 11:43
  • 1
    @RickardElimää that would cause a pretty severe accessibility violation – ksav Jan 17 '23 at 11:45
  • 1
    Many Thanks for your efforts and time! It works and is exactly the answear of my question. Conclusion: My learning is that they do not set the checkbox to display: none in order not to violate accessibility. They just shrink it so you don't see it but it's still there, right? – Max Pattern Jan 17 '23 at 11:56
0
  • Associate the label with the input but putting the input inside it.
  • Style the label to be a block (it is width: auto by default) so it fills the width of its container.

.a {
  display: block;
  background: gray;
  border:1px solid black;
  margin-bottom:5px;
  padding:10px;
  cursor: pointer;
}
<div class="poll">
  <label class="a"><input type="checkbox" /> Yes</div>
  <label class="a"><input type="checkbox" /> No</div>
</div>

Although I thought about using a radio button for a long time. You can only choose one answer.

Then the decision to use a checkbox is simply wrong. Use a radio button. That's the difference between checkboxes and radio buttons.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you Quentin for your answer and your time. Another thank you for your advice to use radios. I really thought about it for a while and decided to do it because I also want to be able to undo my vote. But that would also work with an extra reset button. – Max Pattern Jan 17 '23 at 09:56
  • How can one remove the hook now? Then my question would be answered, so to speak. However, I am also looking at the radio variant. I hope it will be similar and I can remove the selection circle in the same way. – Max Pattern Jan 17 '23 at 09:58
  • ok i think simple with `input { display: none; }` , right? – Max Pattern Jan 17 '23 at 10:00
  • I strongly advise you to keep the regular checkboxes or radio buttons. They are recognisable controls that users understand (e.g. seeing a radio button tells the user they can pick only one option) and which are highly accessible (e.g. colour blindness won't cause people any problems when using them). – Quentin Jan 17 '23 at 10:02
  • I agree with you here! With surveys on the net, you don't actually see the checkboxes but only the labels. So that's exactly my goal. So I thought it was common practice. I will now take the time to design the checkbox (https://moderncss.dev/pure-css-custom-checkbox-style/) and then implement a compromise between beautiful design and accessibility. – Max Pattern Jan 17 '23 at 10:11