javascript - Change value of checkbox based on value of other form -


this question has answer here:

i'm trying have checkbox called 'all' when checked, checks rest of checkboxes in form. have no javascript experience sorry if basic. patched looking @ posts this , this.

<script type="text/javascript"> function checkit(checkbox) {   document.getelementbyid("1").checked = true;   document.getelementbyid("2").click();  } </script> 

my html looks this:

<form>   <input type="checkbox" id="a" onclick="checkit(this)">all<br></input>   <input type="checkbox" id="1">one<br></input>   <input type="checkbox" id="2">two<br></input> </form> 

how can checkboxes 1 , 2 change when select checkbox all? thanks.

since not familiar javascript, suggest looking jquery javascript library. many coders find simpler learn/use, , there no debate requires less typing.

here introductory jquery tutorials if curious.

to solve problem, added class checkboxes wish automatically check/uncheck, , used class check/uncheck boxes.

working jsfiddle here

html:

<form>   <input type="checkbox" id="a">all<br></input>   <input type="checkbox" class="cb" id="1">one<br></input>   <input type="checkbox" class="cb" id="2">two<br></input> </form> 

jquery:

$('#a').click(function() {    // alert($(this).prop('checked'));     if ($(this).is(':checked') == true) {         $('.cb').prop('checked', true);     }else{         $('.cb').prop('checked', false);     } }); 

note solution uses jquery, need jquery library loaded (usually put line in head tags):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -