Access the element before the current element in jQuery

122 Views Asked by At

I have the following HTML table.

<table id="sample_editable_1" role="grid" aria describedby="sample_editable_1_info">
    <tbody>
        <tr role="row" class="odd">
            <td class="sorting_1">
                <input type="text" class="form-control" value=""> </td>
            <td>
                <input type="text" class="form-control" value=""> </td>
            <td>
                <input type="text" class="form-control" value=""> </td>
            <td class="center">
                <button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> </button>
            </td>
        </tr>
    </tbody>
</table>

When I click on the button with class "btn green", I generate a new row by making use of a delegated event handler as follows.

$('#sample_editable_1').on('click', ".btn green", function() {
    $(".odd:last").after('<<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> </button></td></tr>');
});

However, When I create a new row, I need the "add new" button of the previous row to vanish. I can use the hide() function in jQuery, but how do I access the previous row? The following is not working.

$( row ).prev()
4

There are 4 best solutions below

0
On

You can hide() the button which is clicked using this context. Also your selector is incorrect it should be ".btn.green" instead of ".btn green"

$('#sample_editable_1').on('click', ".btn.green", function() {
  $(this).hide();
  //Rest of the code
});

    $('#sample_editable_1').on('click', ".btn.green", function() {
      $(this).hide();
      $(".odd:last").after('<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> Add</button></td></tr>');
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sample_editable_1" role="grid" aria describedby="sample_editable_1_info">
  <tbody>
    <tr role="row" class="odd">
      <td class="sorting_1">
        <input type="text" class="form-control" value="">
      </td>
      <td>
        <input type="text" class="form-control" value="">
      </td>
      <td>
        <input type="text" class="form-control" value="">
      </td>
      <td class="center">
        <button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus">Add</i> 
        </button>
      </td>
    </tr>
  </tbody>
</table>

0
On

Can't you just hide the button which has just been clicked i.e.

    $('#sample_editable_1').on('click', ".btn green", function() {
        $(".odd:last").after('<<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> </button></td></tr>');
        $(this).hide();
    });
0
On

I am found something can you try this one,For more Read This one

$("#addrows").click(function () {
     $("#mytable").each(function () {
         var tds = '<tr>';
         jQuery.each($('tr:last td', this), function () {
             tds += '<td>' + $(this).html() + '</td>';
         });
         tds += '</tr>';
         if ($('tbody', this).length > 0) {
             $('tbody', this).append(tds);
         } else {
             $(this).append(tds);
         }
     });
});
0
On

Try like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sample_editable_1" role="grid" aria describedby="sample_editable_1_info">
<tbody>
<tr role="row" class="odd">
  <td class="sorting_1">
    <input type="text" class="form-control" value="">
  </td>
  <td>
    <input type="text" class="form-control" value="">
  </td>
  <td>
    <input type="text" class="form-control" value="">
  </td>
  <td class="center">
    <button id="sample_editable_1_new" class="btn green"> <i class="fa fa- plus">Add</i> 
    </button>
  </td>
</tr>
</tbody>
</table>

Jquery:

 $('#sample_editable_1').on('click', ".btn.green", function() {
  $(this).hide();
  $(".odd:last").after('<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> Add</button></td></tr>');
});

jsfiddle: https://fiddle.jshell.net/f4ux0bcs/