0

I have a simple table with 5 rows. 1st row is only visible and last 4row are hidden.

<table> 
  <tr> 
   <th>sr</th> 
   <th>Head</th>
  </tr> 
 <tr style="display:block;"> 
   <td>1</td> 
   <td>row 1</td> 
 </tr > 
<tr style="display:none;"> 
   <td>2</td> 
  <td>row 2</td> 
 </tr> 
<tr style="display:none;"> 
   <td>3</td> 
  <td>row 3</td> 
 </tr> 
 <tr style="display:none;"> 
   <td>4</td> 
  <td>row 4</td> 
 </tr> 
 <tr style="display:none;"> 
   <td>5</td> 
  <td>row 5</td> 
 </tr> 
 </table>
 <button id="add">Add row</button>
 <button>Remove row</button>

javascript what i have try

$('#add').click(function() {
rows.show();
});

When click on add row the 2nd row should show and so no and when click on remove row it should hide the latest row and so on

6
  • Show what you've tried. Commented Jun 2, 2017 at 6:01
  • 1
    and where is your javascript code? Commented Jun 2, 2017 at 6:01
  • no, i have not try JavaScript Commented Jun 2, 2017 at 6:02
  • try to solve it first then come back if you stuck in something. SO is not a code me project mate sorry Commented Jun 2, 2017 at 6:04
  • i have tired the bootstrap toggle for hide show but only 2nd row gets toggled Commented Jun 2, 2017 at 6:05

4 Answers 4

1

Just find the first hidden tr using and last visible tr like this

$(document).on('click','#add',function(){

  $('table tbody').find('tr:hidden:first').show();

});
$(document).on('click','#remove',function(){

  $('table tbody').find('tr:visible:last:not(:first-child)').hide();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table> 
  <tr> 
   <th>sr</th> 
   <th>Head</th>
  </tr> 
  <tbody>
 <tr style="display:block;"> 
   <td>1</td> 
   <td>row 1</td> 
 </tr > 
<tr style="display:none;"> 
   <td>2</td> 
  <td>row 2</td> 
 </tr> 
<tr style="display:none;"> 
   <td>3</td> 
  <td>row 3</td> 
 </tr> 
 <tr style="display:none;"> 
   <td>4</td> 
  <td>row 4</td> 
 </tr> 
 <tr style="display:none;"> 
   <td>5</td> 
  <td>row 5</td> 
 </tr> 
 </tbody>
 </table>
 <button id="add">Add row</button>
 <button id="remove">Remove row</button>

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

7 Comments

if my answer is useful mark it with green tick it's useful for future user reference @RahulMohite
how can it prevent 1st row to remove from table
is it possible to prevent 1st row from getting removing
yeah possible just wait a minute @RahulMohite
hey @JYoThI what happend?
|
1

Check if below code works!

I have added id to add and remove buttons.

$('#btnAdd').click(function () { 
  $("table tr:hidden:first").show();
});

$('#btnRemove').click(function () {
   if($("table tr:visible").length !== 2) {
     $("table tr:visible:last").hide();
   }
});

3 Comments

its works but how to prevent 1st row to get removing
Try putting your required use cases in question. Thanks!
oh sure from next time
0

You can use this jQuery function to find hidden tr:

var hiddenTRs = $("tr:hidden");

then you can show of of them with:

hiddenTRs.first().show();

Comments

0

Pure jQuery solution

You could change your buttons

<button data-action="add">Add row</button>
<button data-action="remove">Remove row</button>

and now you can use this script

$(document).ready(function() { 
  $('button[data-action]').on('click', function() {

    // which button is pressed?
    if ($(this).data('action') == 'add') {

      // show 'tr' of all 'td'
      $('table td').parent().css('display', 'block');
    } else {

      // hide'tr' of all 'td'
      $('table td').parent().css('display', 'none');
    }
  });
});

Bootstrap solution

Change your table rows

<tr style="height: 0px;"> 
  <td>5</td> 
  <td>row 5</td>
</tr>

Now you can use this

$(document).ready(function() { 
  $('button[data-action]').on('click', function() {

    // which button is pressed?
    if ($(this).data('action') == 'add') {

      // show 'tr' of all 'td'
      $('table td').parent().collapse('show');
    } else {

      // hide'tr' of all 'td'
      $('table td').parent().collapse('hide');
    }
  });
});

Comments

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.