TheBoubou
TheBoubou

Reputation: 19903

Add /Remove dynamically HTML element with jQuery

I found this great code here the Demo. I just need some change. I'd like

Could you help me ?

Thanks,

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

Upvotes: 3

Views: 13073

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

demo

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

Upvotes: 5

Related Questions