2

In my View, I'm dynamically add divs like this via JS:

<div id="detail[0]">
        <input name="detail.Index" type="hidden" value="0" />
        <input name="detail[0].details_name" type="text" />
        <input name="detail[0].details_value" type="text" />
        <input class="btn" type="button" value="Delete" />
</div>

The question is how to delete the chosen one by clicking the Delete button in div?

JavaScript that adds new divs:

<script>
$(function() {
    var i = 0;
    $('.plus').click(function()
    {
        ++i;
        var html2add = "<div id='detail[" + i + "]'>" +
            "<input name='detail.Index' type='hidden' value='" + i + "' />" +
            "<input name='detail[" + i + "].details_name' type='text' />" +
            "<input name='detail[" + i + "].details_value' type='text' />" +
            "<input class='btn' type='button' value='Delete' />"+
            "</div>";
        $('#details_part').append(html2add);
    })
})

C. America
  • 87
  • 1
  • 2
  • 10

5 Answers5

2

To delete the div containing the delete button, just use the remove() function:

$(document).on('click', '.btn', function(){
  var cur = $(this);
  cur.parent().remove();
});
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Faisal Mk
  • 71
  • 5
1

A node cannot commit suicide (remove itself), therefore you must remove it from its parent node like this:

<script>    
function deleteItem (id) {
      var item = document.getElementById(id);

      item.parentNode.removeChild(item)
    }
</script>
<div>
  <div id="detail[0]">
          <input name="detail.Index" type="hidden" value="0" />
          <input name="detail[0].details_name" type="text" />
          <input name="detail[0].details_value" type="text" />
          <input class="btn" type="button" value="Delete" onclick="deleteItem('detail[0]')" />
  </div>
</div>

Codepen example: https://codepen.io/getreworked/pen/MmVMJB

James
  • 1,045
  • 8
  • 17
0

If you are using Jquery you can do

$(".btn").click(function () {
     $(this).closest("div").remove();
})
OluO
  • 94
  • 3
0

There are many ways to do this.

This one will hide the div using purely JavaScript.

 <div id="detail[0]">
     <input name="detail.Index" type="hidden" value="0" />
     <input name="detail[0].details_name" type="text" />
     <input name="detail[0].details_value" type="text" />
     <input class="btn" type="button" onclick="removeDiv('detail[0]')" value="Delete" />
 </div>

<script>
 function removeDiv(id){
   document.getElementById(id).style.display = 'none';
  }
 </script>

Just pass the parameter of the ID of your parent div.

This is Using jQuery:

 <div id="detail[0]">
     <input name="detail.Index" type="hidden" value="0" />
     <input name="detail[0].details_name" type="text" />
     <input name="detail[0].details_value" type="text" />
     <input class="btn" type="button" value="Delete" />
  </div>

 <script>
 $(function(){
     $('.btn').click(function(){
       $(this).parent().remove();
     });
 });
</script>

Cheers,

smzapp
  • 809
  • 1
  • 12
  • 33
0
// Include below jquery 1.7 script in your file first    
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js">

// then use this code to remove   
<script type="text/javascript">
     $(function(){
         $('.btn').live('click' , function(){
           $(this).parent('div').remove();
         });
     });
</script>