2
I am trying to remove fields from a form that are inside a for
. The problem is that only the first for element is removed, the others do not work.
I think maybe I did something wrong in the function, I have no experience with jQuery. Follow the code below:
THE HTML:
<div id="dados">
@for($i=0; $i<3;$i++)
<span>
<input type="text" name="op1">
<a href="#" id="remover">Remover</a>
</span>
@endfor
</div>
THE JS:
$(document).ready(function(){
$('#remover').click(function(){
$(this).parents('span').remove();
return false;
});
)};
id is unique selector, if you want to create multiple items use an identifier as a class to do. you are only removing the first why click Voce uses this, removing only the span relative of the clicked item, and not all.
– Gabriel Rodrigues
But I don’t want to remove all not, I want to remove only one. for will generate 3 elements. Each element will have a remove button. When you click the remove button I want to remove the corresponding field. The problem is that only the button in the first field works and the others do not.
– Amanda Lima
It worked as I wanted to by changing the
id
forclass
! Thank you– Amanda Lima