0

I'm creating text fields each time a button is clicked, with each new div I create I also want to give it a button to delete this field. As can be seen in this JSFiddle.

However the button associated with each newly created div doesn't delete it's associated field. How can this delete that text fields?

user94628
  • 3,641
  • 17
  • 51
  • 88

2 Answers2

2

You have to use delegation. This works for the elements new to the DOM, after it was already loaded.

$(document).on('click','.deleteButton',function(){
    $(this).closest('.form-group').hide();//remove
});

JSFiddle

Note: I added deleteButton class to the dynamically inserted buttons.

Luigi Siri
  • 2,068
  • 2
  • 19
  • 27
2

You can set in your button an onclick function and handle delete there like following:

window.deleteRow = function(obj){
    $(obj).parent().remove();
}

fiddle

Alex Char
  • 32,879
  • 9
  • 49
  • 70