简体   繁体   中英

how to delete element when click on button only not on all div

how to delete element when press on delete button? but what happened here when i press any where in div the div will delete

i am trying to delete using button only

<script>
  function myexfun(mySelectID, selectedItem, myDIVId) {
    var x = document.getElementById(mySelectID).value;
    $('#parentcur').append('<div id="childcur" class="row mg-l-20 mg-t-40 mg-lg-t-0 "><div class="col-lg-3 mg-t-40 mg-lg-t-0"><label class="rdiobox rdiobox-primary"><input name="rdio" type="radio" checked="" id="rdcur"><span id="selectedItemcur" class="mg-l-20 mg-t-40">' + x + '</span></label></div><div class=" mg-t-40 mg-l-auto mg-lg-t-0"><button class="btn btn-danger btn-sm mg-r-20 mg-t-5 rm" type="button" value="Delete" id="bdd"onClick="delelm()" title="Delete Row"><span class="icon ion-trash-a"></span> Delete</button></div></div>');
    // bd.style.display = "block";

  }
</script>
<script>
  function delelm() {
    $(document).on("click", '#childcur', function() {
      $(this).remove();
    });
  }
</script>

i tried alot but i can not delete it from button just click on div

Your click event listener is attached to the entire div, which is why it deletes whenever you click anywhere inside the div. Since you have delelm() , you can modify the code as below to confine the click to just the delete button alone.

 $('#parentcur').append('<div id="childcur" class="row mg-l-20 mg-t-40 mg-lg-t-0 "><div class="col-lg-3 mg-t-40 mg-lg-t-0"><label class="rdiobox rdiobox-primary"><input name="rdio" type="radio" checked="" id="rdcur"><span id="selectedItemcur" class="mg-l-20 mg-t-40">1</span></label></div><div class=" mg-t-40 mg-l-auto mg-lg-t-0"><button class="btn btn-danger btn-sm mg-r-20 mg-t-5 rm" type="button" value="Delete" id="bdd"onClick="delelm(this)" title="Delete Row"><span class="icon ion-trash-a"></span> Delete</button></div></div>'); function delelm(el) { $(el).closest('#childcur').remove(); }
 <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <div id="parentcur"></div>

Your problem is that you're targeting clicks on the #childcur div and not it's children.

Here's an example of how this could be done:

HTML:

<div id="container">
  <div class="deletable">
    <p>A</p>
    <button class="do-delete">Delete</button>
  </div>

  <div class="deletable">
    <p>B</p>
    <button class="do-delete">Delete</button>
  </div>

  <div class="deletable">
    <p>C</p>
    <button class="do-delete">Delete</button>
  </div>
</div>

JS:

// target buttons within the divs we want to be deletable.
$('div.deletable > button.do-delete').on('click', function() {
    // when the button is clicked, access the parent and remove it from the DOM.
    $(this).parent().remove();
})

Here's a fiddle: https://jsfiddle.net/7aro46et/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM