I have a form with checkbox and I want to keep it checked after submitting the form when it goes back to the same view with an error. I heard that the value attribute can help me to make the checkbox be checked so im trying to set it to true/false. Anyway, the input value stays "false" even if I click it. What happens exactly? I thought the value goes true/false after clicking the checkbox
<input type="checkbox" name="acceptRules" class="inline checkbox" id="checkbox1" value="false">
<script>
$("#checkbox1").is(':checked', function(){
$("#checkbox1").attr('value', 'true');
});
</script>
.getAttribute('checked')
doesn't return anything, even though you would apply the 'checked' attribute to the HTML to set it's initial state. The fix is to access the JS property of the input, also namedchecked
. I.e in a vanilla JS handler for the change event of a check-box, this does nothing:byId('tgt').value = this.getAttribute('checked');
, while this sets the text to true or false:byId('tgt').value = this.checked;
– enhzflep