lucas neves
lucas neves

Reputation: 11

Append Another Div

I'm trying to append another form-group. The button at the form-group added is not working. Can anyone tell me why?

 <div class="form-group">
  <input type="text"/>
  <button class="add">Add</button>
 </div>

JS

 $('.add').click(function(){
 $('.form-group').append(
 '<input type="text"/>'+
 '<button class="add">Add</button>'
 );
})

Upvotes: 1

Views: 52

Answers (1)

j08691
j08691

Reputation: 207861

You need to use event delegation via .on() for dynamically added elements:

$('.form-group').on('click', '.add', function() {
  $('.form-group').append(
    '<input type="text"/>' +
    '<button class="add">Add</button>'
  );
})

jsFiddle example

Upvotes: 4

Related Questions