-1

I have three checkboxes and I'm trying to implement "All" and "None".

<div class="controls col-md-5">
     <input id="None" name="None" type="checkbox" value="true" /><input name="None" type="hidden" value="false" />
     <label for="None">None</label>
</div>

The internet says to check and set the 'checked' property, just like you would expect.

if ($('#None').checked) {
    $('#cBox').checked = false;
}

But it doesn't work. Checking the console, I can see that $('#None').checked is returning 'undefined'.
$('#None').attr('checked) should also work but also returns 'undefined'.
$('#None') is working. I can expand it and see the 'checked' prop is updating as I check and uncheck the box.

UPDATE
$('#None').is(':checked') works!
$('#None').attr(':checked') is still undefined.

Now I just need to set 'checked' on the three boxes, but nothing works.

$('#None').attr('checked')=true fails with 'you can't assign to a function call.'
$('#None').checked=true doesn't fail, but doesn't change anything.

BWhite
  • 713
  • 1
  • 7
  • 24
  • 1
    [How to test if a checkbox is checked](https://stackoverflow.com/questions/901712/how-do-i-check-whether-a-checkbox-is-checked-in-jquery), and [how to check a checkbox](https://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery). – Don't Panic May 02 '21 at 08:13
  • 1
    Does this answer your question? [Setting "checked" for a checkbox with jQuery](https://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery) – Don't Panic May 02 '21 at 08:13

1 Answers1

0

The solution was to look closer at the node that opens in the console for $('#None').

if ($('#None')[0].checked) {
    $('#cBox')[0].checked = false;
}

works just fine.
I went ahead and posted this question since the answer was so simple and so hard to find.

BWhite
  • 713
  • 1
  • 7
  • 24