Theranil
Theranil

Reputation: 3

Add,Remove Dynamic rows with Jquery

I have done with adding and removing dynamic rows in html, jquery.

jsfiddle.net/gansai/p5QwC/1/

HTML:

<form action="grading.php" method="post">               
<table width="100%" id="tableRealizzazione">
    <tr>
        <th></th>
        <th style="padding:12px;text-align:center;">Serial No</th>
        <th style="padding:12px;text-align:center;">Personale</th>
        <th style="padding:12px;text-align:center;">Percentage</th>
        <th style="padding:12px;text-align:center;">Marketing point</th>
        <th style="padding:12px;text-align:center;">Add/Remove</th>
    </tr>

    <tr class="even">
        <td></td>
        <td>
            <input type="text" name="serial[]"  class="add increment" value="1">
        </td>
        <td style="padding:12px;">
            <input type="text" value="" name="Personale[]">
        </td>
        <td style="padding:12px;">
            <input type="text" name="from[]" size="5%"> -
            <input type="text" name="to[]" size="5%"> %
        </td>
        <td style="padding:12px;">
            <input type="text" class="totaliCostiGestione" name="marketpt[]">
        </td>
        <td style="padding:12px;">
            <input type="text" name="programid[]" class="add" value="34" size="10%">
        </td>
        <td style="padding:12px;">
            <input type="button" class="addnew add" value="+" />
        </td> 
    </tr>
    <tr>
        <td>
            <input type="submit" name="submit" value="submit">
        </td>
    </tr>                     
</table>

jQuery:

    $('.addnew').live('click', function(){
var thisRow = $(this).parent().parent();
newRow = thisRow.clone(true).insertAfter(thisRow);
newRow.find('input:not(.add)').val("");

$(this).removeClass('addnew').addClass('remove');
    $(this).val("-");
    newRow.find('input.increment').val(parseInt(thisRow.find('input.increment').val())+1);
});

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

​ ​ But I want add/remove actions to the same button. Instead of appearing remove icon to the previous row.

Can anyone suggest?

Upvotes: 0

Views: 3857

Answers (1)

gotohales
gotohales

Reputation: 3695

I believe this is what you are looking for, but let me know if it isn't.

    $('.addnew').live('click', function(){
    var thisRow = $(this).parent().parent();
    newRow = thisRow.clone(true).insertAfter(thisRow);
    newRow.find('input:not(.add)').val("");

    newRow.find('.addnew').removeClass('addnew').addClass('remove');
    newRow.find('.remove').val("-");
    newRow.find('input.increment').val(parseInt(thisRow.find('input.increment').val())+1);
});

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

This keeps the + on the first row and adds - buttons to the cloned rows.

http://jsfiddle.net/p5QwC/3/ for a working example.

UPDATE

Assuming you don't want the initial row to be removable, maybe this is more what you are looking for. http://jsfiddle.net/p5QwC/10/

Upvotes: 1

Related Questions