php - Button to select check boxes based on start of string - Javascript -


i have form has users listed , each user has checkboxes below names. want have button each user selects of users checkboxes.

here's dynamically created button code , dynamically created checkboxes. function included has form name, start of checkbox ids, , checkbox value arguments.

<input type="button" onclick="setallcheckboxes('form1', '<? echo $row['id']; ?>_', true);" value="check all">  <input name="<? echo $userid?>_<? echo $checkboxnumber ?>" type="checkbox" id="<? echo $userid?>_<? echo $checkboxnumber  ?>" value="1" /> 

i need onclick function click of desired users checkboxes when clicked. think i'll need form of regular expression somewhere select example: of checkboxes start 12_ or 15_

if give button name user id part of checkbox names, do:

<button type="button" name="userfoo" onclick="setcheckboxes(this)" ...>check 'em</button> 

then in function:

function setcheckboxes(element) {   var re = new regexp('^' + element.name + '_');   var controls = element.form.elements;    (var i=0, ilen=controls.length; i<ilen; i++) {     if (re.test(controls[i].name)) {       controls[i].checked = true;     }   } } 

some test html:

<form>   <button name="foo" type="button" onclick="setcheckboxes(this)">check 'em</button>   <input type="checkbox" name="foo_1">1   <input type="checkbox" name="foo_2">2   <input type="checkbox" name="foo_3">3   <br>   <button name="bar" type="button" onclick="setcheckboxes(this)">check 'em</button>   <input type="checkbox" name="bar_1">1   <input type="checkbox" name="bar_2">2   <input type="checkbox" name="bar_3">3 </form> 

if put class on each of checkboxes user id, function might be:

function setcheckboxes(element) {   var controls = element.form.queryselectorall('input.' + element.name);   (var i=0, ilen=controls.length; i<ilen; i++) {     controls[i].checked = true;   } } 

or in modern browsers (but not ie8):

function setcheckboxes(element) {   var controls = element.form.queryselectorall('input.' + element.name);   [].foreach.call(controls, function(el){el.checked = true}); } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -