As Jon said id will create problem for you.
Think you have 4 lis with id like specific_0, specific_1, specific_2,
specific_4 and two variable values are specific_count = 3, specific_id=3,
Now think you suppose removed li with id specific_2, now you have 3 lis
with ids specific_0, specific_1 and specific_3 and two variable values
are specific_count = 2, specific_id=2;
Now if you add another element then you will have 4 lis with ids
specific_0, specific_1, specific_3, specific_3
As when you removed specific_2 that time you didn't updated the id
specific_3 to specific_2 and this will happen when you keep removing and
adding again and again and you will end up having repeated ids for more
than one elements.
Now if you click on remove button of last li with id specific_3, it will
remove first li with id specific_3 as your ids are repeated.
So get rid of ids, you can do it without id very easily like
- $(document).ready(function() {
- $('#add_specific').click(function(){
-
$('#specifics').append('<li"><input
type="text" class="code"
name="SpecificLabel[]" value=""
placeholder="Label" /> <input type="text"
class="code" name="SpecificData[]"
value="" placeholder="Data" /><button
class="remove_specific"><strong>-</strong></button></li>');
- });
-
-
$('#specifics').on('click',
'.remove_specific', function(event){
- $(this).closest("li").remove();
- });
- });