Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing dynamically created divs with jquery

EDIT: Just so I asks questions better next time. I got a -1 what did I do wrong?

I'm adding quite a lot of html dynamically and want to be able to remove them also one by one.

Below the JQuery. I want to be able to remove the whole $("#row1' + (counter) + '") div including all divs in it by clicking the remove button under it.

Here's a fiddle: http://jsfiddle.net/FullContCoder/Sf9r5/1/

Thanks a lot in advance!

$(document).ready(function() {

    var counter = 0;

    $(".addButton").click(function() {

    var test =    $( '<div id="row1' + (counter) + '" class="row"><div class="col-md-2"><p><small>Placement Name:</small></p><input id="inputPlacement' + (counter) + '" type="text" class="form-control input-sm" name="placementName" placeholder="ex: Homepage">' +
            '</div><div class="col-md-3"><p><small>URL:</small></p><input id="inputURL" type="url" class="form-control input-sm" name="url" placeholder="ex: http://www.allure.com"></div>' +
            '<div class="col-md-2"><div class="row"><div class="col-md-12"><p><small>Select a Date and Time:</small></p></div></div><div class="row"><div class="col-md-12">' +
            '<div class="input-append date" id="dp3" data-date="12-02-2012" data-date-format="dd-mm-yyyy"><input class="span2" size="16" type="text" value="12-02-2012" readonly=""><span class="add-on"><i class="icon-calendar"></i>' +
            '</span></div></div></div><div class="row"><div class="input-append bootstrap-timepicker"><input id="timepicker1" type="text" class="input-small"><span class="add-on">' +
            '<i class="glyphicon glyphicon-time"></i></span></div></div></div><div class="col-md-2"><p><small>Recurring:</small></p><select id="recurring" class="form-control input-sm">' +
            '<option name="daily" value="daily">Daily</option><option name="weekly" value="weekly">Weekly</option><option name="month" value="month">Month</option></select></div>' +
            '<div class="col-md-2"><p><small>Day of Week:</small></p><div class="row"><div class="col-md-6"><label class="checkbox-inline"><input id="mon" name="test" type="checkbox" value="1"> Mon</label>' +
            '</div><div class="col-md-6"><label class="checkbox-inline"><input id="fri" name="test" type="checkbox" value="1"> Fri</label></div></div><div class="row"><div class="col-md-6">' +
            '<label class="checkbox-inline"><input id="tue" name="test" type="checkbox" value="1"> Tue</label></div><div class="col-md-6"><label class="checkbox-inline">' +
            '<input id="sat" name="test" type="checkbox" value="1"> Sat</label></div></div><div class="row"><div class="col-md-6"><label class="checkbox-inline">' +
            '<input id="wen" name="test" type="checkbox" value="1"> Wed</label></div><div class="col-md-6"><label class="checkbox-inline">' +
            '<input id="sun" name="test" type="checkbox" value="1"> Sun</label></div></div><div class="row"><div class="col-md-6"><label class="checkbox-inline">' +
            '<input id="thu" name="test" type="checkbox" value="1"> Thu</label></div></div></div><div class="col-md-1"><p><small>Remove:</small></p>' +
            '<button type="button" class="removeButton btn btn-default btn-sm"><span class="glyphicon glyphicon-minus-sign"></span></button></div></div>' )



        $('.removeButton').click(function() {
            $("#row1' + (counter) + '").remove();
        });

        counter++;


       $("#row1").after(test);
    });




});
like image 548
FullContactCoder Avatar asked Apr 11 '14 01:04

FullContactCoder


People also ask

How to remove dynamic div in jQuery?

First, if you have the requirement to delete only child elements of a selected element (div/span/..) then use empty(). Second, if you want to delete a selected element with all its child elements then use remove(). In this way jQuery helps to remove child nodes, emptying a div, table, span, etc.

How do you delete a dynamic element?

var A = document. getElementById('affiliation'); A. parentNode. removeChild(A);

How to remove and add div using jQuery?

click(function() { $(this). parent('div'). remove(); });

How to dynamically add div in jQuery?

jQuery example to dynamically add a div on page In this example, we have a div with id board and a button to add new divs into this board. In $(document). ready() we have bound an event listener to click event, which means when the user will click on this button, new divs will be created and added into the board.


2 Answers

You should use event delegation to add events to dynamically created elements. And use .closest() to get the first element that matches the selector. Try this:

$(document).on('click','.removeButton',function() {
     $(this).closest("div.row").remove();
});

You need to move your remove button click handler outside of add button click handler.

DEMO

like image 199
Kiran Avatar answered Oct 26 '22 11:10

Kiran


You can try it.

$(document).on('click', '.removeButton', function(){
    $('#row1' + counter).remove();
});
like image 24
lighter Avatar answered Oct 26 '22 12:10

lighter