2

I have construted my table as follows:

    <table id="dataTable">
            <thead>
                <tr><th>Name</th>
               <th>Value</th></tr>
            </thead>
<TR><TD>Scooby Doo</TD><TD>6</TD><TD><INPUT TYPE="Button"  onClick="AddRow()" VALUE="Add Row"></TD></TR>
</table>

When the button Add Row is clicked, I need to change the button to a delete button and insert a new row on the first line. The first line must contain the same as in the code. How can I do this?

On clicking the delete button, Imust be able to delete the row to which the delete button belong?

2 Answers 2

8

Hope this helps

$(function(){
  $("input[type='button'].AddRow").toggle(
     function(){
       var el = $(this);
       el.closest('tr').clone(true).prependTo(el.closest('table'));
       el.attr("value", "Delete row");
     },
     function(){ 
       $(this).closest('tr').remove();          
  });
});

<table id="dataTable" border="1">
    <thead>
        <tr>
            <th>
                Name
            </th>
            <th>
                Value
            </th>
        </tr>
    </thead>
    <tr>
        <td>
            Scooby Doo
        </td>
        <td>
            6
        </td>
        <td>
            <input type="Button" value="Add Row" class="AddRow">
        </td>
    </tr>
 </table>

Working Demo

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

Comments

1

Something like?

$('#dataTable thead').prepend('<tr><th>Name</th><th>Value</th></tr>');

And for the deleting:

$('#dataTable thead tr').click(function() {
$(this).hide(); //hide the row, this doesn't delete it.
});

.

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.