0

I currently ran into a problem that i cannot remove the appended html with jQuery.

What works now is i can append the html that i want and it looks nice, but removing does not work. It works only on reload the first loaded html the others that i append are not working - I don't know why.

Here is my code for appending the html:

$("#adWasteStream").click(function(){
    var newTxt = $('<div class="row-waste-container width-100"><div class="row"><div class="col-md-9"><p>Stream 1</p></div><div class="col-md-3"><button class="btn btn-add-waste deleteWasteStream"><i class="fa fa-minus-circle o-btn-add" aria-hidden="true"></i>Remove Waste Stream</button></div><div class="form-group col-md-7"><label for="business-mng-name" class="control-label">Waste Stream</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="Co-mingled recycling"><span class="help-block"></span></div></div><div class="row width-100"><div class="form-group col-md-4"><label for="business-mng-name" class="control-label">Volume (m <sup>3</sup>)</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="50"><span class="help-block"></span></div><div class="form-group col-md-4"><label for="business-mng-name" class="control-label">Weight (t)</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="20"><span class="help-block"></span></div><div class="form-group col-md-4"><label for="business-mng-name" class="control-label">Cost ($)</label><input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="400"><span class="help-block"></span></div></div></div>');
    $(".row-cont-main").append(newTxt);
});

My code for removing:

$(".deleteWasteStream").click(function(){
      var curRow = $(this).parents('div.row-waste-container');
      curRow.remove();
});

I don't need to use Id's as they need to be unique all elements (for back-end purposes).

The structure of the html looks like this:

<div class="row-waste-container width-100">
                                    <div class="row">
                                        <div class="col-md-9">
                                            <p>Stream 1</p>
                                        </div>
                                        <div class="col-md-3">
                                            <button class="btn btn-add-waste deleteWasteStream"><i class="fa fa-minus-circle o-btn-add" aria-hidden="true"></i>Remove Waste Stream</button>
                                        </div>
                                        <div class="form-group col-md-7">
                                            <label for="business-mng-name" class="control-label">Waste Stream</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="Co-mingled recycling">
                                            <span class="help-block"></span>
                                        </div>
                                    </div><!-- end row -->
                                    <div class="row width-100">
                                        <div class="form-group col-md-4">
                                            <label for="business-mng-name" class="control-label">Volume (m <sup>3</sup>)</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="50">
                                            <span class="help-block"></span>
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label for="business-mng-name" class="control-label">Weight (t)</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="20">
                                            <span class="help-block"></span>
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label for="business-mng-name" class="control-label">Cost ($)</label>
                                            <input type="text" class="form-control form-input-field" name="business-mng-name" value="" required="" placeholder="400">
                                            <span class="help-block"></span>
                                        </div>
                                    </div><!-- end row -->
                                </div><!-- end row-waste container -->
Muli
  • 214
  • 5
  • 23
  • Possible duplicate of [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Carsten Løvbo Andersen Feb 01 '18 at 08:23

3 Answers3

2

You need to change your click event handler to handle also dynamically appended html.

Update your remove click handler with the code below

$("body").on('click' , '.deleteWasteStream' , function(){
      var curRow = $(this).parents('div.row-waste-container');
      curRow.remove();
});
Amr Labib
  • 3,995
  • 3
  • 19
  • 31
1

You cant use .click() on dynamic objects/html. You can use .on()

$("body").on('click', '.deleteWasteStream', function( event ) {
    var curRow = $(this).parents('div.row-waste-container');
    curRow.remove();
});

Documentation: http://api.jquery.com/on/

Eddie
  • 26,593
  • 6
  • 36
  • 58
0

Since the elements were created dynamically, they are not registered in DOM like pre-rendered elements (those rendered when your webpage is loaded in the browser). The best solution is binding the dynamically created element to the prerendered ones using the on event binder

$('.row-cont-main').on('click','.deleteWasteStream',function(){
     var curRow = $(this).parents('div.row-waste-container');
     curRow.remove();
});
Silverman42
  • 111
  • 7