2

hi iam try to checked a checkbox when user clicking on checkbox and when is checked add attribute checked and when is not checked remove attribute checked and when is checked set value="on" and when is not checked set attribute value="off" and passing result to php code for save in db iam writing a code bot not working

$(document).ready(function() {

  function checking() {

    var checkbox = $('.checkbox');
    var check = $(checkbox).is('checked', true);
    if (check) {
      // $(checkbox).removeAttr('checked',true);
      $(checkbox).prop('checked', true);
    } else {
      //  $(checkbox).attr('checked',false);

      $(checkbox).prop('checked', false);

    }
  }



});
guradio
  • 15,524
  • 4
  • 36
  • 57
  • 3
    https://stackoverflow.com/questions/2204250/check-if-checkbox-is-checked-with-jquery should be duplicated question – Xun Mar 12 '19 at 02:35
  • try something like `$(".checkbox").change(function() { $(this).attr("value", $(this).is(":checked") ? "no" : "off"); }).change()` – guradio Mar 12 '19 at 02:36

1 Answers1

0

Remember that jQuery selectors return a list, not a single element. If what you have above is what you are going for, something like this should do:

$(document).ready(function() {
  var checkbox = $('.checkbox')[0];
  var check = checkbox.is(':checked');
  if (check) {
    $(checkbox).prop('checked', true);
  } else {
    $(checkbox).prop('checked', false);
  }
});
LetsBeFrank
  • 774
  • 11
  • 31
  • I changed the code according to your guidance, but this time I do not allow the slave to check the checkbox https://stackoverflow.com/users/1053845/letsbefrank – mehdi Mollazehi Mar 12 '19 at 07:57