javascript - Buttons to select checkboxes by name -


i have following checkboxes in html form.

<ul id="selection">   <span><label>first<br><input type="checkbox" name="first"></label></span>   <span><label>second<br><input type="checkbox" name="second"></label></span>   <span><label>third<br><input type="checkbox" name="third"></label></span>   <span><label>fourth<br><input type="checkbox" name="fourth"></label></span>   <span><label>fifth<br><input type="checkbox" name="fifth"></label></span> </ul> 

i'm trying create 2 buttons. 1 select first 2 , 1 select of them.
managed select of them 1 button using this:

<input type="button" class="check btn" onclick="select(this)" value="select all"/>     <script>       $('.check:button').click(function(){           var checked = !$(this).data('checked');           $('input:checkbox').prop('checked', checked);           $(this).val(checked ? 'uncheck all' : 'select' )           $(this).data('checked', checked);       });     </script> 

but can't seem 2 buttons working.
javascript knowledge pretty limited , been searching days, unsuccessfully solution works.
can please help? detailed explanation awesome.

try this:

$("#selectall").click(function() {   $("input[type='checkbox']").prop('checked', true); }); $("#selecttwo").click(function() {   $("input[name='first'], input[name='second']").prop('checked', true); }); $("#resetall").click(function() {   $("input[type='checkbox']").prop('checked', false); }); 

demo


Comments