javascript - Jquery DOM setting checked in checkbox works for true not false -


i trying set checked value of checkbox incoming data mongo. works fine if value true, when value false, still checks box. can tell me wrong.

<input type="checkbox" id="chk" name="interested" value="{{inmate.interested}}" onclick="chkset(this)">not interested <br/> <input type="checkbox" id="chk" name="correspondence" value="{{inmate.correspondence}}" onclick="chkset(this)">req correspondence<br/> <input type="checkbox" id="chk" name="study" value="{{inmate.study}}" onclick="chkset(this)">request study<br/> <input type="checkbox" id="chk" name="specialtyitemsapproved" value="{{inmate.specialtyitemsapproved}}" onclick="chkset(this)">specialty items approved<br/> <br>  $(document).ready(function(){      document.getelementsbyname("interested")[0].checked=document.getelementsbyname("interested")[0].value;     document.getelementsbyname("correspondence")[0].checked=document.getelementsbyname("correspondence")[0].value;     document.getelementsbyname("study")[0].checked=document.getelementsbyname("study")[0].value;     document.getelementsbyname("specialtyitemsapproved")[0].checked=document.getelementsbyname("specialtyitemsapproved")[0].value;  }); 

document.getelementsbyname("interested")[0].checked=document.getelementsbyname("interested")[0].value; sets checked property based on value of element, string. coerce string boolean. non-blank strings truthy, both "true" , "false" set checked true.

if use == test, can set checked accordingly:

document.getelementsbyname("interested")[0].checked =     document.getelementsbyname("interested")[0].value == "true"; 

that said, purpose of value of checkbox in html/dom not indicate whether it's checked, setting value "true" or "false" in first place not want do. purpose of value value should sent form if checkbox checked. example:

<input type="checkbox" name="roomoptions" value="non-smoking"> <input type="checkbox" name="roomoptions" value="with-kitchen"> <input type="checkbox" name="roomoptions" value="en-suite"> 

the form have roomoptions=non-smoking if checkbox checked, and/or roomoptions=with-kitchen if that checkbox checked, and/or roomoptions=en-suite if that checkbox checked. if none of them checked, form won't have roomoptions sent @ all. 3 sent if 3 checkboxes checked.

separately, cannot use same id on more 1 element in html/dom document. ids must unique. can't use id="chk" on of checkboxes.

so suspect want more this:

<input type="checkbox" id="chk-interested" name="interested" {{#if inmate.interested}}checked{{/if}} onclick="chkset(this)">not interested <br/> <input type="checkbox" id="chk-correspondence" name="correspondence" {{#if inmate.correspondence}}checked{{/if}}" onclick="chkset(this)">req correspondence<br/> <input type="checkbox" id="chk-study" name="study" {{#if inmate.study}}checked{{/if}} onclick="chkset(this)">request study<br/> <input type="checkbox" id="chk-specialty-items-approved" name="specialtyitemsapproved" {{#if inmate.specialtyitemsapproved}}checked{{/if}} onclick="chkset(this)">specialty items 

then don't need javascript @ all.

i didn't put value on those, means when form sent in (if you're sending in form), value interested , such server receive default value "on". e.g., form either not have interested field @ (the checkbox wasn't checked), or have interested=on.

note unless use ids something, can leave them off; it's name form use when submitted. made them unique demonstrate must that.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -