0

I have the following script and I am trying to disable the remove, move up and down buttons if only one row is present. Could someone show me how to do this please, I am stuck?

Can anyone show me how to add incremental names to a select field using the scrip below?
Thank You

<script>
$('.addnew').live('click', function(){
var thisRow = $(this).parent().parent();
newRow = thisRow.clone(true).insertAfter(thisRow);
newRow.find('input:not(.add)').val("");
newRow.find('.remove').show();
newRow.find('input.increment').val(parseInt(thisRow.find('input.increment').val())+1);
});

$('.remove').live('click', function(){
$(this).parent().parent().remove();

});

$('.up,.down').click(function () {

  var row = $(this).parents('tr:first');

  if ($(this).is('.up')) {

        row.insertBefore(row.prev());

  }

  else {

        row.insertAfter(row.next());

  }

  });
  </script>
Alsjka
  • 55
  • 1
  • 7

1 Answers1

0

This is from another answer by tvanfosson:

Use a selector that will select all the rows and take the length.

var rowCount = $('#myTable tr').length;

I don't know what your HTML looks like, but by the way you are selecting rows above:

var rowCount = $(this).parents('tr').length;

So, only execute your code to disable the buttons if that length is equal to one. Note that it will count all trs of every nested table, so you may be checking for a different number other than 1.

Community
  • 1
  • 1
egl
  • 167
  • 1
  • 1
  • 8
  • 1
    Thanks for leading me in the right direction; though I tried your solution it did not work. but I managed to implement this = "var rowCount = $(this).closest("tr").prevAll("tr").length; //get table rows if(rowCount>0) //leave starting row inplace" – Alsjka Dec 17 '13 at 21:06