I am trying to make a form similar to the attached picture, for my website.
The target is to allow the user to check only one minus and one plus from one block. So far, my html and css code has been perfectly fine but then I found some issues which should not happen, such as once a checkbox has been selected and deselected, it can be selected again even though another option from the same row has been selected.
My html and css code is:
<!DOCTYPE html>
<html>
<head>
<title>My HTML page</title>
<style>
/* style all checkboxes */
input[type="checkbox"] {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
border: 1px solid #999;
border-radius: 3px;
padding: 5px;
}
/* style unchecked checkboxes */
input[type="checkbox"]:not(:checked) {
background-color: #fff;
}
/* style checked plus checkboxes */
input[type="checkbox"][value="plus"]:checked {
background-color: #000000;
}
/* style checked minus checkboxes */
input[type="checkbox"][value="minus"]:checked {
background-color: #000000;
}
/* disable other plus checkboxes when a plus checkbox is checked */
input[type="checkbox"][value="plus"]:checked ~ input[type="checkbox"][value="plus"]:not(:checked) {
opacity: 0.5;
pointer-events: none;
}
/* disable other minus checkboxes when a minus checkbox is checked */
input[type="checkbox"][value="minus"]:checked ~ input[type="checkbox"][value="minus"]:not(:checked) {
opacity: 0.5;
pointer-events: none;
}
</style>
</head>
<body>
<form>
<p>Question 1:</p>
<input type="checkbox" name="q1" value="plus"> Plus
<input type="checkbox" name="q1" value="minus"> Minus
<p>Question 2:</p>
<input type="checkbox" name="q2" value="plus"> Plus
<input type="checkbox" name="q2" value="minus"> Minus
<p>Question 3:</p>
<input type="checkbox" name="q3" value="plus"> Plus
<input type="checkbox" name="q3" value="minus"> Minus
<p>Question 4:</p>
<input type="checkbox" name="q4" value="plus"> Plus
<input type="checkbox" name="q4" value="minus"> Minus
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
What I’m trying to achieve is to allow the user to select one plus (+) from the row and one minus (-) from the row in the block.
Based on the image, the user can choose one option from the 1,3,5 and 7 row and one option from the 2, 4, 6 and 8 row.
Can someone please advice me how to overcome this issue? Thank you in advance!!!
2
Answers
you can achieve this simply using some script aid.
Here billyonecan’s reply is what you want.
He creates groups and dont let more than one checkbox is checked. Here is the example
Here script part you have to include
Edit: Dont forget to include jquery on your page. You can include jQuery from CDN (Content Delivery Network)
Radio inputs exist for this exact purpose. Just change the
type
of theinput
elements fromcheckbox
toradio
and it should work. For multiple radio inputs with the samename
only one can be selected. You will have to do some styling if you want your radios to look like checkboxes.