Use this to remove an element from the div

1

The detail is this, when I click (xClones) = all the elements of #campoBusq are deleted and the question is how it is done so that only the element that is clicked is deleted

<div id="campoBusq">
         <div class="elemento">
             texto1 <div class="xClones"> X </div> 
         </div>

         <div class="elemento">
             texto2 <div class="xClones"> X </div>
          </div>

         <div class="elemento">
             texto3 <div class="xClones"> X </div>
         </div>
     </div>
$('#campoBusq').on('click','.xClones',function() {
   $("#campoBusq").find(".elemento").remove();*Como podria usar un this aqui*

});

    
asked by Gamez 18.03.2017 в 00:20
source

3 answers

1

Surely it does not work like that?

$('#campoBusq').on('click','.xClones',function() {
     $(this).parent().remove();
});
    
answered by 18.03.2017 / 00:26
source
1
  

How is it done to remove only the item that is clicked ?

Plain and simple with remove :

$(this).remove();

Or if you want to delete the parent ( .elemento ):

$(this).parent().remove();
    
answered by 18.03.2017 в 00:25
1

Simply use parent(); and give it the name of the element in the following way:

$('#campoBusq').on('click', '.xClones', function() {
  $(this).parent("div").remove();
});
*{
 font-family: Arial;
}

#campoBusq{
  width: 60px;
}

.xClones{
  float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="campoBusq">
  <div class="elemento">
    texto1
    <div class="xClones"> X </div>
  </div>

  <div class="elemento">
    texto2
    <div class="xClones"> X </div>
  </div>

  <div class="elemento">
    texto3
    <div class="xClones"> X </div>
  </div>
</div>
    
answered by 18.03.2017 в 00:39