-5

I made code to generate html rows.

How do I delete them now?? THis doesn't work

 $('.deleteEnv').click(function () {
   $(this).parents().remove();
});

$('#addEnv').click(function() {
  $('#envVariablesDiv').append('<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Name</label><input id="envName" class="form-control" name="envName" type="text" placeholder="e.g. name1" /></div></div>' +
    '<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Variable</label><input class="form-control" id="envVar" type="text" name="envVar" placeholder="e.g. var1" /></div></div><div class="col-sm-2 top10"><button type="button" class="btn btn-danger deleteEnv"><span class="fa fa-trash">Delete</span></button></div>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="addEnv">Add</button>
<div id="envVariablesDiv">

</div>
qweq
  • 107
  • 1
  • 5
  • 2
    You already asked this question. https://stackoverflow.com/q/48441993/1288408 Why ask again? – Modus Tollens Jan 25 '18 at 12:21
  • 1
    @qweq I'd strongly suggest you read some guides on how this site works, for example ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic), ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask), and more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). This isn't the first time you've asked multiple duplicate questions with very little research effort, and it's not the first time you've thrown abusive language at those attempting to help you. – Rory McCrossan Jan 25 '18 at 12:42

4 Answers4

0

You need to setup the click event each time you have appended the HTML to the DOM.

At the moment you're loading the page, assigning a delete event to nothing, then when a user clicks to add a new item, it adds some html, and the delete event is not assigned.

Chris Morris
  • 878
  • 2
  • 15
  • 32
0

This Is You Want :

$(document).on('click','#addEnv',function(){
  $('#envVariablesDiv').append('<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Name</label><input id="envName" class="form-control" name="envName" type="text" placeholder="e.g. name1" /></div></div>' +
    '<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Variable</label><input class="form-control" id="envVar" type="text" name="envVar" placeholder="e.g. var1" /></div></div><div class="col-sm-2 top10"><button type="button" class="btn btn-danger deleteEnv"><span class="fa fa-trash">Delete</span></button></div>');
});


$(document).on('click','.deleteEnv',function(){
  $('#envVariablesDiv').empty();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >


<button id="addEnv">Add</button>
<div id="envVariablesDiv">

</div>
Saurabh Mistry
  • 8,903
  • 4
  • 33
  • 51
0

Insert all your code inside a div(it's easier this way), and use a delegated event handler:

$('#addEnv').click(function() {
  var parentDiv = $('<div/>', {
    class : 'parentDiv',
    html : '<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Name</label><input id="envName" class="form-control" name="envName" type="text" placeholder="e.g. name1" /></div></div>' +
    '<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Variable</label><input class="form-control" id="envVar" type="text" name="envVar" placeholder="e.g. var1" /></div></div><div class="col-sm-2 top10"><button type="button" class="btn btn-danger deleteEnv"><span class="fa fa-trash">Delete</span></button></div>'
  });
  $('#envVariablesDiv').append(parentDiv);
});

$('#envVariablesDiv').on('click', '.deleteEnv', function(){
    $(this).parent().parent().remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="addEnv">Add</button>
<div id="envVariablesDiv">

</div>
KL_
  • 1,473
  • 1
  • 6
  • 13
0

Change some parts of your code to the following, this is bad way to code, but for now it works.

$('#deleteEnv').click(function () {
                $(this).parents().remove();
                console.log("clicked");
                });
            $('#addEnv').click(function() {
                $('#envVariablesDiv').append('<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Name</label><input id="envName" class="form-control" name="envName" type="text" placeholder="e.g. name1" /></div></div>' +
                    '<div class="col-sm-5 top10"><div class="input-group"><label class="input-group-addon">Variable</label><input class="form-control" id="envVar" type="text" name="envVar" placeholder="e.g. var1" /></div></div><div class="col-sm-2 top10"></div>');
                $('#deleteEnv').show();
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="addEnv">Add</button>
 <div id="envVariablesDiv"></div>
 <button type="button" class="btn btn-danger" style="display:none;" id = "deleteEnv"><span class="fa fa-trash">Delete</span></button>
Harsha
  • 99
  • 1
  • 6