0

To generate dynamic check boxes I am using map like this

{{range $key, $val := .package.Group_Name_Map}}
  <div class="row">
    <div class="col-xs-12 col-sm-3 col-md-3">
        <label><strong>{{$val}}</strong></label>
    </div>              
    <div class="col-xs-12 col-sm-3 col-md-3">
        <input type="checkbox" name="read">
    </div>
    <div class="col-xs-12 col-sm-3 col-md-3">
        <input type="checkbox" name="write">
    </div>
    <div class="col-xs-12 col-sm-3 col-md-3">
        <input type="checkbox" name="update">
    </div>

  </div>
{{end}}

now i want these checkboxes to be predefined by dynamically got values. This i have done for beego framework

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Vijay Kumar
  • 597
  • 2
  • 8
  • 27

3 Answers3

0
var statement = true;
if (statement) {
    $('input[type=checkbox]').prop('checked', true);
}

http://jsfiddle.net/r0o8kxvo/1/

This will select all input's that are checkboxes and make them checked if the statement is true

statement is a placeholder for what you call

dynamically got values

online Thomas
  • 8,864
  • 6
  • 44
  • 85
0

In HTML Just add disabled="disabled" as an attribute to disable and checked="checked" as attribute to checked as default

<input type="checkbox" name="update" checked> checked
<input type="checkbox" name="update" disabled> disabled
<input type="checkbox" disabled="disabled" checked="checked"> disable and checked

in jQuery use prop() function

$('.checks').prop("checked",true);
$('.checks1').prop("disabled",true);
$('.enable').prop("disabled",false);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" class="checks" name="update"> checks1
    <input type="checkbox" class="checks1" name="updates"> disabled
    <input type="checkbox" class="enable" name="updates"> enable
Sathish
  • 2,440
  • 1
  • 11
  • 27
0

Try something like this:

1.Dynamically create checkbox

$('#btn').click(function () {
    var cb1 = "<input type='checkbox' class='cb1' value='cb1'>I am a checkbox.";
    $("#panel").html(cb1);
});

2.Dynamically enable/disable checkbox

$('#ble').click(function () {
    var stat = $(".cb1").is(":disabled");
    if (stat) {
        $(".cb1").prop('disabled', false);
    } else {
        $(".cb1").prop('disabled', true);
    }
});

3.Dynamically check/uncheck the checkbox

$('#check').click(function () {
    var stat = $(".cb1").is(":checked");
    if (stat) {
        $(".cb1").prop('checked', false);
    } else {
        $(".cb1").prop('checked', true);
    }
});

DEMO

Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97