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'/>    <button class='btn btn-danger' onclick='remove()' >Sil</button>   <button class='btn btn-success' onclick='createSelection()'>Şık Ekle</button></div></div></br>").insertBefore("#btn")
}
function remove() {$("#question").remove()}
            Asked
            
        
        
            Active
            
        
            Viewed 74 times
        
    -2
            
            
         
    
    
        Get Off My Lawn
        
- 34,175
- 38
- 176
- 338
 
    
    
        Bilal Günaydın
        
- 1
- 4
- 
                    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 Answers
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"/>    
    <button class="btn btn-danger">Sil</button>   
    <button class="btn btn-success">Şık Ekle</button>
  </div></br>
</div> 
    
    
        ibrahimyilmaz
        
- 2,317
- 1
- 24
- 28
- 
                    Simpler to use `$(this)` than needing the event and target for `$(e.target)` – charlietfl Mar 23 '19 at 23:08
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'/>    <button class='btn btn-danger' >Sil</button>   <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
- 
                    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:24
- 
                    Not working. You know what I want to do? I want to erase the div I want to erase and I want it erased. I don't want to start deleting the first created div. – Bilal Günaydın Mar 23 '19 at 20:33
- 
                    Bilal bey, there was a typo I had closeset , instead of closest, i corrected it – Ashkan Mobayen Khiabani Mar 23 '19 at 20:34
- 
                    
- 
                    
- 
                    
- 
                    
- 
                    This works. Thank you so much. But I want to ask another problem this time. The page stays below when I delete the div. Can every div deleted page pull up the page? How do I get a div that is on top instead of a deleted div? – Bilal Günaydın Mar 24 '19 at 16:29
- 
                    Can I also click on the Save button instead of the div I removed up to the location? Can all divs be deleted when they are first? – Bilal Günaydın Mar 24 '19 at 16:31