lucas lucas - 1 year ago 91
Javascript Question

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>


'<input type="text"/>'+
'<button class="add">Add</button>'

Answer Source

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

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

jsFiddle example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download