-1

My code dynamically creates new fields, but I am only able to remove the first created field. All other attempts to call the remove function seem to fail and I have no idea why.

Fiddle: https://jsfiddle.net/n2fole00/25xfht1y/

HTML

<div class="acal-position-container">
    <div class="acal-card-container">
        <input type="hidden" name="id[]" value="">
        <div style="background-color:#ADD8E6;padding:15px;margin-top:15px;">
            <div style="text-align:right;margin-bottom:10px;">
                <a href="#" class="remove-position-card"><strong>Remove position </strong></a>
            </div>
            <div class="form-group">
                <label for="title[]">Title</label>
                <input type="text" name="title[]" value="" class="form-control" required>
            </div>
        </div>
    </div>
</div>

<input type="button" id="add_position_card" style="margin-top:5px;" value="Add position">

jQuery

$('.remove-position-card').click(function () {
    $(this).closest('.acal-card-container').remove();
    //console.log('called');
});

$('#add_position_card').click(function () {
    $('.acal-position-container').append('<div class="acal-card-container">' +
  '  <input type="hidden" name="id[]" value="">' +
  '  <div style="background-color:#ADD8E6;padding:15px;margin-top:15px;">' +
  '    <div style="text-align:right;margin-bottom:10px;">' +
  '      <a href="#" class="remove-position-card"><strong>Remove position </strong></a>' +
  '    </div>' +
  '    <div class="form-group">' +
  '      <label for="title[]">Title</label>' +
  '      <input type="text" name="title[]" value="" class="form-control" required>' +
  '    </div>' +
  '  </div>' +
  '</div>');
});

Thanks.

Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
user3442612
  • 1,802
  • 6
  • 22
  • 27
  • 3
    The *reason* the answers work is because if you're adding DOM elements dynamically you have to either (a) add the click handlers to them explicitly (tedious and error-prone), or (b) add the click handler on a containing element and allow the click events to "bubble up" (you can Google that), eliminating the need to add the handler to each element. This is also more efficient, because there's only one handler instead of one per element. – Dave Newton Mar 29 '16 at 11:27

2 Answers2

2

As you are generating html dymamically, use event delegation.

Instead of

$('.remove-position-card').click(function() {

Use

$('.acal-position-container').on('click','.remove-position-card' , function() {

Fiddle

Docs

Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

You must use event delegation for dynamically created elements.

$('.acal-position-container').on('click','.remove-position-card' ,function() {
  $(this).closest('.acal-card-container').remove();
  //console.log('called');
});

Working Fiddle

Reference

Rino Raj
  • 6,264
  • 2
  • 27
  • 42
  • As `.acal-position-container` is present on page load so instead of `$(document)`, `$('.acal-position-container')` would be better selector as shown in my answer. – Kartikeya Khosla Mar 29 '16 at 11:33