1

I'm trying to hide table rows when a tickbox is checked. I have managed to get it working, but it will only hide the first instance of the id. Could someone direct me in to changing the js so it will hide all with matching id.

JSFIDDLE

 $(document).ready(function() {
   $('#checkbox1').change(function() {
     if (!this.checked)

       $('#tierPoints').fadeIn('slow');
     else
       $('#tierPoints').fadeOut('slow');
   });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td colspan=2>
      <input type="checkbox" id="checkbox1">Show/Hide</td>
  </tr>
  <!-- End Of Row -->

  <tr id="tierPoints">
    <td>71</td>
    <td>1000</td>
  </tr>
  <!-- End Of Row -->

  <tr id="tierPoints">
    <td>73</td>
    <td>2000</td>
  </tr>
  <!-- End Of Row -->

  <tr id="tierPoints">
    <td>75</td>
    <td>3000</td>
  </tr>
  <!-- End Of Row -->

</table>
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
Wayne Gilliver
  • 125
  • 1
  • 12

6 Answers6

2

In HTML, an id has to be unique. See www.w3.org... for more details.

Try to change id="tiersPoints" to class="tiersPoints".

War10ck
  • 12,387
  • 7
  • 41
  • 54
rageberry
  • 21
  • 3
1

You need to have unique ids. Use classes in this case:

$(document).ready(function() {
   $('#checkbox1').change(function() {
     if (!this.checked)

       $('.tierPoints').fadeIn('slow');
     else
       $('.tierPoints').fadeOut('slow');
   });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td colspan=2>
      <input type="checkbox" id="checkbox1">Show/Hide</td>
  </tr>
  <!-- End Of Row -->

  <tr class="tierPoints">
    <td>71</td>
    <td>1000</td>
  </tr>
  <!-- End Of Row -->

  <tr class="tierPoints">
    <td>73</td>
    <td>2000</td>
  </tr>
  <!-- End Of Row -->

  <tr class="tierPoints">
    <td>75</td>
    <td>3000</td>
  </tr>
  <!-- End Of Row -->

</table>
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
1

An id should be distinct within your page.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td colspan=2>
      <input type="checkbox" id="checkbox1">Show/Hide</td>
  </tr>
  <!-- End Of Row -->

  <tr id="tierPoints1">
    <td>71</td>
    <td>1000</td>
  </tr>
  <!-- End Of Row -->

  <tr id="tierPoints2">
    <td>73</td>
    <td>2000</td>
  </tr>
  <!-- End Of Row -->

  <tr id="tierPoints3">
    <td>75</td>
    <td>3000</td>
  </tr>
  <!-- End Of Row -->

</table>
R. McIntosh
  • 166
  • 4
1

Here is solution:

As i mentioned in comment : You need to use tierPoints class. You cannot you multiple tr with same id

$(document).ready(function () {
    $('#checkbox1').change(function () {
        if (!this.checked) 
        
        $('.tierPoints').fadeIn('slow');
        else 
        $('.tierPoints').fadeOut('slow');
    });
});
input[type=checkbox]{padding:0; margin:0;}


td, th {
  background: #ddd;
  color: #000;
  padding: 2px 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td colspan=2><input type="checkbox" id="checkbox1">Show/Hide</td>
</tr>
<!-- End Of Row -->

<tr class="tierPoints">
<td>71</td>
<td>1000</td>
</tr>
<!-- End Of Row --> 

<tr class="tierPoints">
<td>73</td>
<td>2000</td>
</tr>
<!-- End Of Row --> 

<tr class="tierPoints">
<td >75</td>
<td>3000</td>
</tr>
<!-- End Of Row --> 

</table>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • 1
    Thank you all. I could not see what I had done wrong... so obvious now... my bad... I cant mark every answer as accepted, so thank you all – Wayne Gilliver Jan 13 '17 at 15:18
1

Try this. Change id by class. Id is unique by definition.

$(document).ready(function() {
   $('#checkbox1').change(function() {
     if (!this.checked)

       $('.tierPoints').fadeIn('slow');
     else
       $('.tierPoints').fadeOut('slow');
   });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td colspan=2>
      <input type="checkbox" id="checkbox1">Show/Hide</td>
  </tr>
  <!-- End Of Row -->

  <tr class="tierPoints">
    <td>71</td>
    <td>1000</td>
  </tr>
  <!-- End Of Row -->

  <tr class="tierPoints">
    <td>73</td>
    <td>2000</td>
  </tr>
  <!-- End Of Row -->

  <tr class="tierPoints">
    <td>75</td>
    <td>3000</td>
  </tr>
  <!-- End Of Row -->

</table>
Nordine
  • 824
  • 7
  • 25
1

You need to use the "class" selector instead of ID Selector and the ID needs to be unique. You can have multiple elements with same Class Name.

     $(document).ready(function() {
       $('#checkbox1').change(function() {
         if (!this.checked)

           $('.tierPoints').fadeIn('slow');
         else
           $('.tierPoints').fadeOut('slow');
       });
     });
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr>
        <td colspan=2>
          <input type="checkbox" id="checkbox1">Show/Hide</td>
      </tr>
      <!-- End Of Row -->

      <tr class="tierPoints">
        <td>71</td>
        <td>1000</td>
      </tr>
      <!-- End Of Row -->

      <tr class="tierPoints">
        <td>73</td>
        <td>2000</td>
      </tr>
      <!-- End Of Row -->

      <tr class="tierPoints">
        <td>75</td>
        <td>3000</td>
      </tr>
      <!-- End Of Row -->

    </table>
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41