25
votes

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>
7
I'm confused by this question. It seems your wanting to change the value if the checkbox is checked. But traditionally the values of checkboxes do not change. Their state determines if they are passed to the backend or not.Taplar
It's worth pointing out, that when one toggles a checkbox, the element's HTML doesn't actually change. Thus, doing .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 named checked. 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
Are you saying that on page 1 you optionally can select a checkbox and then submit, and on the second page you want the checked state to persist from how it was on the first form?Taplar
I just need to return an error message if the checkbox is not clicked. DamndivHelper11

7 Answers

37
votes

If I understand the question, you want to change the value of the checkbox depending if it is checked or not.

Here is one solution:

$('#checkbox-value').text($('#checkbox1').val());

$("#checkbox1").on('change', function() {
  if ($(this).is(':checked')) {
    $(this).attr('value', 'true');
  } else {
    $(this).attr('value', 'false');
  }
  
  $('#checkbox-value').text($('#checkbox1').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<input type="checkbox" name="acceptRules" class="inline checkbox" id="checkbox1" value="false">

<div id="checkbox-value"></div>
9
votes

Use Checked = true

$("#checkbox1").prop('checked', true);

Note: I am not clear whether you want to onclick/onchange event on checkbox. is(":checked", function(){}) is a wrong in the question.

6
votes

To return true or false depending on whether a checkbox is checked or not, I use this in JQuery

let checkState = $("#checkboxId").is(":checked") ? "true" : "false";

2
votes

Try this

$("#checkbox1").is(':checked', function(){
  $("#checkbox1").prop('checked', true);
});
      
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="acceptRules" class="inline checkbox" id="checkbox1" value="false">
1
votes

Checkboxes can be really weird in JS. You're best off checking for the presence of the checked attribute. (I've had older jQuery versions return true even if checked is set to 'false'.) Once you've determined that something is checked then you can get the value from the value attribute.

1
votes

jQuery.is() function does not have a signature for .is('selector', function).

I guess you want to do something like this:

      if($("#checkbox1").is(':checked')){
          $("#checkbox1").attr('value', 'true');
      }
1
votes

I'm going to post this answer under the following assumptions. 1) You (un)selected the checkbox on the first page and submitted the form. 2) Your building the second form and you setting the value="" true/false depending on if the previous one was checked. 3) You want the checkbox to reflect if it was checked or not before.

If this is the case then you can do something like:

var $checkbox1 = $('#checkbox1');
$checkbox1.prop('checked', $checkbox1.val() === 'true');