0

I'm looking for a way to use JavaScript in the following piece of code. I want the checkbox with id = alles to check all the available checkboxes that are printed during the while loop.

<form id="andere" name="andere" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
      <fieldset><legend>Selecteer de klassen waaraan je de persoon wilt koppelen:</legend>
        <table>
          <tr>
            <td><input type="checkbox" id="alles" name="alles" value="">Selecteer alle klassen</td>
          </tr>
          <tr>
<?php
  $query='SELECT oc_klas.klascode, oc_klas.klas_ID FROM oc_klas ORDER BY oc_klas.klas_ID';
  $result=mysqli_query($mysqli,$query) or die('<p>Kan query: '.$query.' niet uitvoeren.</p>');
  $aantalRijen = mysqli_num_rows($result);
  $KLASSEN_PER_RIJ = 10;
//bereken de colspan van de laatste kolom
  $fill=$aantalRijen%$KLASSEN_PER_RIJ;

  $fill=$KLASSEN_PER_RIJ-$fill;
  $teller=1;

  while($myrow=mysqli_fetch_row($result))
  {
    print('<td><input type="checkbox" id="klas'.$myrow[1].'" name="klas'.$myrow[1].'" value="'.$myrow[1].'">'.$myrow[0].'</td>');
    if($teller%$KLASSEN_PER_RIJ==0)
    {
      print('</tr><tr>');
    }
    $teller++;
  }
  print('</tr></table>');
?>

Usually I rely on the onclick function:

onclick="for(c in document.getElementsByName('klas')) document.getElementsByName('klas').item(c).checked = this.checked"

but since the ID's are not identical now, I'm not sure how to tackle this particular problem. Any ideas?

Update with a solution given but not working

<form id="andere" name="andere" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
      <fieldset><legend>Selecteer de klassen waaraan je de persoon wilt koppelen:</legend>
        <table>
          <tr>
            <td><input type="checkbox" id="alles" name="alles" value="" onclick="for(c in document.getElementsByClassName('alleklassen')) document.getElementsByName('klas').item(c).checked = this.checked">Selecteer alle klassen</td>
          </tr>
          <tr>
<?php
  $query='SELECT oc_klas.klascode, oc_klas.klas_ID FROM oc_klas ORDER BY oc_klas.klas_ID';
  $result=mysqli_query($mysqli,$query) or die('<p>Kan query: '.$query.' niet uitvoeren.</p>');
  $aantalRijen = mysqli_num_rows($result);
  $KLASSEN_PER_RIJ = 10;
//bereken de colspan van de laatste kolom
  $fill=$aantalRijen%$KLASSEN_PER_RIJ;

  $fill=$KLASSEN_PER_RIJ-$fill;
  $teller=1;

  while($myrow=mysqli_fetch_row($result))
  {
    print('<td><input type="checkbox" class="alleklassen" id="klas'.$myrow[1].'" name="klas'.$myrow[1].'" value="'.$myrow[1].'">'.$myrow[0].'</td>');
    if($teller%$KLASSEN_PER_RIJ==0)
    {
      print('</tr><tr>');
    }
    $teller++;
  }
  print('</tr></table>');
?>
3
  • 2
    I would echo a class name out on all the checkboxes that are part of the "whole" set. Then select them all with something like document.getElementsByClassName("mycheckbox"); Commented Nov 30, 2015 at 16:14
  • 1
    if you were using jquery, it'd be as simple as $('.somecssclass').prop('checked', true) Commented Nov 30, 2015 at 16:16
  • Well, he did ask for Javascript.... Commented Nov 30, 2015 at 16:30

1 Answer 1

2

Assign a class to all the checkboxs and use document.getElementsByClassName would be the simplest solution.

Sign up to request clarification or add additional context in comments.

3 Comments

Haha, beat me to it x)
I updated the OP with your solution. It's not working though, what did I do wrong?
<td><input type="checkbox" id="alles" name="alles" value="" onclick="for(c in document.getElementsByClassName('alleklassen')) document.getElementsByClassName('alleklassen')[c].checked = this.checked">Selecteer alle klassen</td>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.