3

I've searched and I could not find an answer to solve my problem here. I am fairly noobish with jQuery, and I am looking to add/remove input fields from two different areas. I've tried editing my jQuery and HTML code to accommodate the second part I want to add/remove (the items found), but I cannot get it to work. Any help would be greatly appreciated!

jQuery:

<script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd2').click(function() {
                var num2        = $('.clonedInput2').length;    //     how many "duplicatable" input fields we currently have
                var newNum  = new Number(num2 + 1);     // the numeric ID of the new input field being added

            // create the new element via clone(), and manipulate it's ID using newNum value
            var newElem2 = $('#input2' + num2).clone().attr('id', 'input2' + newNum);

            // manipulate the name/id values of the input inside the new element
            newElem2.children(':first').attr('id', 'name' + newNum).val(null);

            // insert the new element after the last "duplicatable" input field
            $('#input2' + num2).after(newElem2);

            // enable the "remove" button
            $('#btnDel2').attr('disabled','');

        });

        $('#btnDel2').click(function() {
            var num = $('.clonedInput2').length;    // how many "duplicatable" input fields we currently have
            $('#input2' + num).remove();        // remove the last element

            // enable the "add" button
            $('#btnAdd2').attr('disabled','');

            // if only one element remains, disable the "remove" button
            if (num-1 == 1)
                $('#btnDel2').attr('disabled','disabled');
        });

        $('#btnDel2').attr('disabled','disabled');
    });
</script>

HTML:

<form id="myForm" action="process_call.php" method="post">
        <div id="input1" style="margin-bottom:4px;" class="clonedInput">
            Charge: <input type="text" name="name[]" id="name1" />
        </div>
        <div>
            <input type="button" id="btnAdd" value="Add Another Charge" />
            <input type="button" id="btnDel" value="Remove Charge" />
        </div>

        <div id="input2" style="margin-bottom:4px;" class="clonedInput2">
            Item Found: <input type="text" name="item[]" id="item1" />
        </div>
        <div>
            <input type="button" id="btnAdd2" value="Add Another Item" />
            <input type="button" id="btnDel2" value="Remove Item" />
        </div>
        <input type="submit">
    </form>

1 Answer 1

3

The problem was the selector $('#input2' + num2) when the script is executed first, there is only one element with id input2, but your selector is looking for an element with id input21 which does not exits.

I fixed it by cloning the last element with class clonedInput2 instead of finding the element with id

jQuery(function($) {
    $('#btnAdd2').click(function () {
        var num2 = $('.clonedInput2').length; //     how many "duplicatable" input fields we currently have
        var newNum = num2 + 1; // the numeric ID of the new input field being added

        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem2 = $('.clonedInput2:last').clone().attr('id', 'input2' + newNum);

        // manipulate the name/id values of the input inside the new element
        newElem2.children(':first').attr('id', 'name' + newNum).val(null);

        // insert the new element after the last "duplicatable" input field
        $('.clonedInput2:last').after(newElem2);

        // enable the "remove" button
        $('#btnDel2').prop('disabled', false);

    });

    $('#btnDel2').click(function () {
        var num = $('.clonedInput2').length; // how many "duplicatable" input fields we currently have
        $('#input2' + num).remove(); // remove the last element

        // enable the "add" button
        $('#btnAdd2').attr('disabled', '');

        // if only one element remains, disable the "remove" button
        if (num - 1 == 1) $('#btnDel2').attr('disabled', 'disabled');
    });

    $('#btnDel2').attr('disabled', 'disabled');
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you much! I had to make a couple small edits to fix the remove button not working with your code, but works excellent!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.