-2

how to make a checkbox selected if the data in the database is yes or not, I tried with the attr function but that didn't work

 if(data[1].grace == 'yes'){
    console.log('this is yes');
    $("#grace").attr("checked",true);
 }else{
    console.log('this is no');
 }

this my html

<div class="string-check string-check-bordered-primary mb-2">
    <input type="checkbox" name="grace" class="form-check-input" id="grace">
    <label class="string-check-label" for="grace">
        <span class="ml-2" for="grace"></span>
    </label>
</div>
Kiran Dash
  • 4,816
  • 12
  • 53
  • 84
misry
  • 341
  • 2
  • 4
  • 14
  • 1
    could be related to https://stackoverflow.com/questions/15044340/jquery-set-checkbox-checked – albert Dec 06 '19 at 03:25
  • Does this answer your question? [jquery set checkbox checked](https://stackoverflow.com/questions/15044340/jquery-set-checkbox-checked) – chandu komati Dec 06 '19 at 03:27
  • No need for jQuery here tough. `document.getElementById('grace').checked = true` If I look at this now, could this be your issue? You give the string `"true"` and not the boolean `true` – The Fool Dec 06 '19 at 03:32

2 Answers2

-1

Try to trigger the checkbox instead

$("#grace").trigger('click');
PHCris
  • 38
  • 9
-1

Have you imported jQuery properly? Any error in the console?

https://codepen.io/fuleinist/pen/PowqgRY This is working for me.

$(document).ready(function() {
  $("#grace").attr("checked",true);
});
Chris Chen
  • 1,228
  • 9
  • 14