Add / remove dynamically HTML element using jQuery

I found this great code here . Demo . I just need some kind of change. I would like to

  • one button per line to delete the current line
  • confirmation before deletion

could you help me?

Thank,

Update 1: I would like this alt text http://imagik.fr/thumb/275405.jpeg

+3
source share
1 answer

demonstration

HTML

<form id="myForm">
    <div style="margin-bottom:4px;" class="clonedInput">
        <input type="button" class="btnDel" value="Delete" disabled="disabled" />
        <input type="text" name="input1" />
    </div>

    <div>
        <input type="button" id="btnAdd" value="add another name" />
    </div>
</form>​

JQuery

$(document).ready(function() {

    var inputs = 1; 

    $('#btnAdd').click(function() {
        $('.btnDel:disabled').removeAttr('disabled');
        var c = $('.clonedInput:first').clone(true);
            c.children(':text').attr('name','input'+ (++inputs) );
        $('.clonedInput:last').after(c);
    });

    $('.btnDel').click(function() {
        if (confirm('continue delete?')) {
            --inputs;
            $(this).closest('.clonedInput').remove();
            $('.btnDel').attr('disabled',($('.clonedInput').length  < 2));
        }
    });


});

Resources

+5
source

Source: https://habr.com/ru/post/1757282/


All Articles