i have loop in jquery ($.each
) creates list of checkboxes.
i'm trying insert checked
:
$.each(arridreg, function (id, idregx) { if (idregx[0] == idregy[0]) { nom = nom + idregx[1] + ", "; // strchecked = "checked='checked'"; strchecked = true; } }); $('#elen').append("<li><input type='checkbox' value='" + idregx[0] + " name='idreg' class='checkbox'>" + idregx[1] + "</li>").prop('checked', strchecked);
the problem not work, because selected.
can me, please?
thanks!
the return value of .append()
element appended to, code trying set checked
property on $("#elen")
, not checkbox.
the simplest way add checked
attribute in html you're appending.
$.each(arridreg, function (id, idregx) { if (idregx[0] == idregy[0]) { nom = nom + idregx[1] + ", "; var strchecked = "checked='checked'"; } else { strchecked = ""; } $('#elen').append("<li><input type='checkbox' value='" + idregx[0] + " name='idreg' class='checkbox' " + strchecked + ">" + idregx[1] + "</li>"); });
if want more programmatically, can create checkbox separate step.
$.each(arridreg, function (id, idregx) { if (idregx[0] == idregy[0]) { nom = nom + idregx[1] + ", "; var boolchecked = true; } else { boolchecked = false; } var checkbox = $("<input>", { type: 'checkbox', value: idregx[0], name: 'idreg', 'class': 'checkbox', checked: boolchecked }); $('#elen').append($("<li>").append(checkbox).append(idregx[1])); });