Rick Cannon
Rick Cannon

Reputation: 139

JQuery append on click of a button

I have a form which does a small calculation and display data in table rows. I have also added a button to remove each row at the end. But when I do the calculation couple of times the button gets duplicated. Please refer to the image below.

Image

JS Fiddle

Following is how I append the button

    $(document).ready(function () {
    $('#calculate').click(function () {
    var removeBtn = $('<button class="removeBtn" id="removeBtn">Remove</button>');
                    $('.row-time').append(removeBtn);

        });
    });

I don't understand why the button gets duplicated everytime I add a new row.

Upvotes: 0

Views: 62

Answers (1)

4b0
4b0

Reputation: 22323

Use last for last class.

$('.row-time:last').append(removeBtn);

Working Fiddle

Upvotes: 2

Related Questions