rc...@panix.com (Pierre Jelenc) wrote on 04 Dec 2020 in
comp.infosystems.www.authoring.stylesheets:
> In article <
Q8mdnTXeL8q26FTC...@westnet.com.au>,
> Andrew Poulos <
ap_...@hotmail.com> wrote:
>>
>>Is there some CSS I can add to a collection of checkboxes that stops all
>>of them from being unselected? So if the user is unselecting checkboxes
>>the last selected checkbox can't be unselected.
>
> It's too late, I won't try to test it, but I think if you place all your
> checkboxes one after the other, as siblings, and the submit button as the
> last sibling, you can use the :checked pseudo-class to hide and unhide the
> submit button so they can't submit until at least one box is checked.
>
>#submit-button {display: none;}
> .box:checked ~ #submit-button {display: inline-block;}
>
> Something like that.
>
> Pierre
<script>
const minimal = (t) => {
const boxes = t.querySelectorAll('input[type=checkbox]');
const boxesChecked =
t.querySelectorAll('input[type=checkbox]:checked');
if (boxesChecked.length == 1)
boxesChecked[0].disabled = true;
else
boxes.forEach(
function(box) { box.disabled = false }
);
};
</script>
<form onchange='minimal(this)'>
<input type=checkbox checked disabled> 1<br>
<input type=checkbox> 2<br>
<input type=checkbox> 3<br>
<input type=submit>
</form>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)