-2
function create() {
    $("<div class='question container' id='question' style='background-color:lavender; border:solid 15px darkcyan; height:500px; border-radius:32px'><div class='input-group' style='margin-top:10px'><input type='text' placeholder='Soru Gir...' class='form-control'/> &nbsp; &nbsp;<button class='btn btn-danger' onclick='remove()' >Sil</button>&nbsp; &nbsp;<button class='btn btn-success' onclick='createSelection()'>Şık Ekle</button></div></div></br>").insertBefore("#btn")
}

function remove() {$("#question").remove()}
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • just call the function that you created... `remove()` – Get Off My Lawn Mar 23 '19 at 20:15
  • @GetOffMyLawn That's how I wipe it off. I'm creating more than one div. When I click on the button, I press the button to delete the first created the div. I want him to catch that the div and remove which button I'm pressing. How can I do that? – Bilal Günaydın Mar 23 '19 at 20:20

2 Answers2

1

Is that what you want? There are many things about your code. Here is some:

  • Don't use recurring id attributes.
  • Listen event like my example. This also affects dynamically added elements.

$('body').on('click', '.btn-danger', function(e) {
    $(this).closest('.question').remove();
});

$('body').on('click', '.btn-success', function(e) {
    var $quest = $(this).closest('.question');
    $quest.clone().insertAfter($quest);
});
.question {
    background-color: lavender;
    border: solid 15px darkcyan;
    border-radius: 32px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="question container">
  <div class="input-group" style="margin-top: 10px">
    <input type="text" placeholder="Soru Gir..." class="form-control"/> &nbsp; &nbsp;
    <button class="btn btn-danger">Sil</button>&nbsp; &nbsp;
    <button class="btn btn-success">Şık Ekle</button>
  </div></br>
</div>
ibrahimyilmaz
  • 2,317
  • 1
  • 24
  • 28
0

function create() {
    $("<div class='question container' style='background-color:lavender; border:solid 15px darkcyan; height:500px; border-radius:32px'><div class='input-group' style='margin-top:10px'><input type='text' placeholder='Soru Gir...' class='form-control'/> &nbsp; &nbsp;<button class='btn btn-danger' >Sil</button>&nbsp; &nbsp;<button class='btn btn-success' onclick='createSelection()'>Şık Ekle</button></div></div></br>").insertBefore("#btn");
}
$('body').on('click','.question .btn-danger', function(){
     $(this).closest('.question').remove();
}); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<input type="button" id="btn" value="create" onclick="create();" />

Add this to your javascript code:

$('body').on('click','.question .btn-danger', function(){
     $(this).closest('.question').remove();
});
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171