heinrich666
heinrich666

Reputation: 3

add and remove row from table using button

I would like to have a table with 10 rows 5 of which are visible. Also 2 buttons - and + and if clicked a row is added or removed, same as the + image in the bbc.co.uk site. How would i accomplish this

Upvotes: 0

Views: 1364

Answers (2)

Hannes
Hannes

Reputation: 8237

Just add Listeners and use remove & append:

<table id="table">
    <tr>
        <td>foo</td>
    </tr>
    <tr>
        <td>bar</td>
    </tr>
</table>
<span id="plus">+</span>
<span id="minus">-</span>

<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script>
$(document).ready(function () {
    $('#minus').click(function(){
        $('#table tr:last-child').remove();
    });
    $('#plus').click(function(){
        $('#table').append('<tr><td>new</td></tr>');
    });
});
</script>

Upvotes: 2

nokiko
nokiko

Reputation: 491

I made you a quick demo here. You didn't say you wanted to keep the old ones as they do on the BBC site so I kept that feature out JSFiddle

Upvotes: 1

Related Questions