Problem to remove element added with jQuery

Asked

Viewed 1,293 times

6

I have on a page one input, a button to add more inputs and each has a corresponding button to remove it.

The problem is I can’t remove the inputs added via button. Only the original is removed. Follow the code here and running on jsfiddle

HTML:

<div id="content">
<button class="add">Adicionar outro campo</button>
<br><br>
<span>
    <input type="text" name="nome" value="Original"> 
    <button class="apagar">Apagar </button>
    <br>
</span>
</div>

JS:

$(document).ready(function(){
    $('.add').click(function(){
        $('#content').append('<span> <input type="text" name="nome" value="Cópia"> <button class="apagar">Apagar </button></span> <br> ');
    });

    $('.apagar').click(function(){
        $(this).parents('span').remove();
    });
});
  • The problem is the scope, put the delete inside the add click that will work.

  • Thanks, you helped me out

3 answers

6


you are adding the event click to input.apagar before creating the same.

then put the evento to all the input.apagar pertaining to #content using the method $.on.

Another detail, the $.parents will fetch all relatives, so if in your HTML tree you have more than one span above the input.apagar it will also be deleted, so use the method $.parent, After all you only want the first span.

$(document).ready(function(){
    var add = $('.add');
    var content= $('#content');
  
    add.click(function(){
        content.append('<div><span> <input type="text" name="nome" value="Cópia" /> <button class="apagar">Apagar </button></span></div>');
    });

    content.on("click", "button.apagar", function(event){
        $(event.target).parent().parent().remove();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="content">
  <div>
    <button class="add">Adicionar outro campo</button>
  </div>
  <div>
    <span>
      <input type="text" name="nome" value="Original" /> 
      <button class="apagar">Apagar </button>
    </span>
  </div>
</div>

here is an adaptation of the @Gabrielrodrigues solution

$(document).ready(function() {
  var add = $(".add");
  var content = $('#content');

  var onApagarClick = function() {
    $(this).parents('span').remove();
  };

  add.click(function() {
    var novaLinha = $('<span> <input type="text" name="nome" value="Cópia"> <button class="apagar">Apagar </button></span> <br>');    
    $('.apagar', novaLinha).click(onApagarClick);
    $('#content').append(novaLinha);        
  });

  $('.apagar').click(onApagarClick);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="content">
  <div>
    <button class="add">Adicionar outro campo</button>
  </div>
  <div>
    <span>
      <input type="text" name="nome" value="Original" /> 
      <button class="apagar">Apagar </button>
    </span>
  </div>
</div>

4

The problem is the scope, the delete click only this making effect outside the add click, and it is in the add that a new field is inserted.

$(document).ready(function() {
  $('.add').click(function() {
    $('#content').append('<span> <input type="text" name="nome" value="Cópia"> <button class="apagar">Apagar </button></span> <br> ');
  });
  
  $("#content").on('click', '.apagar', function(e) {
    $(this).parents('span').remove();
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="content">
  <button class="add">Adicionar outro campo</button>
  <br>
  <br>
  <span>
	<input type="text" name="nome" value="Original"> 
	<button class="apagar">Apagar </button>
	<br>
</span>
</div>

  • 1

    following his recommendation, whenever he add a input.apagar it will make a new attach of an event click to all the input.apagar, then if it adds 4 input.apagar and click on the first one he added the method will run 4 times.

  • @Tobymosque edited.

  • I updated my reply with an adaptation of your reply.

3

Pass the event directly:

$(document).ready(function() {
  $('body').on('click', '.add', function() {
    $('#content').append('<span> <input type="text" name="nome" value="Cópia"> <button class="apagar">Apagar </button></span><br>  ');
  });

  $('body').on('click', '.apagar', function() {
    $(this).parents('span').remove();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">
<button class="add">Adicionar outro campo</button>
<br><br>
<span>
	<input type="text" name="nome" value="Original"> 
	<button class="apagar">Apagar </button>
	<br>
</span>
</div>

  • 2

    use the .on directly on elements such as $(document), $('body') is rarely a good idea... always try to define a smaller scope, to solve the problem of AP the ideal scope would be the $('#content')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.