Havia
Havia

Reputation: 15

append div does not work

I have this button with a div tag

<div class='test'>
    <button class="bt1">add statment</button>
</div>

I want to append the div by jquery bellow it the function

$(".test").on("click", ".bt1", function () {
    $(".test").append("<p>Statement: <input type='text' name='statement"+ i +"'><br><button class='deleteCon'> Delete</button></p>"); 
    i++;
});

Any one can tell me what is the problem?

Upvotes: 0

Views: 64

Answers (2)

Jacob
Jacob

Reputation: 1483

I believe you are using append correctly but it appears that i was not defined.

See the following javascript:

var i = 0;
$('.bt1').on('click', function() {
    $('.test').append("<p>Statement: <input type='text' name='statement"+ i +"'><br><button class='deleteCon'> Delete</button></p>");
    i++;
});

http://jsfiddle.net/xg5Bu/

Upvotes: 0

Wilfredo P
Wilfredo P

Reputation: 4076

Your problem is that i is not declare on the scope:

var i = 1; //Declare
$(".test").on("click", ".bt1", function () {
            $(".test").append("<p>Statement: <input type='text' name='statement"+ i +"'><br><button class='deleteCon'> Delete</button></p>"); i++; });

Live Demo

Upvotes: 1

Related Questions