1

I am creating a dynamic form where a person is able to add/remove extra charfields, much like "Attach Another File" forms. For reference:

HTML

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
    <p>
        <label for="p_scnts">
            <input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
        </label>
    </p>
</div>

JAVASCRIPT

$(function () {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function () {
        $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i + '" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
        i++;
        return false;
    });

    $('#remScnt').live('click', function () {
        if (i > 2) {
            $(this).parents('p').remove();
            i--;
        }
        return false;
    });
});

http://jsfiddle.net/jaredwilli/tZPg4/4/

Problem is I need to reindex the Field Names and IDs in case the person deletes a middle field. That is, if person adds extra fields 0,1,2,3 and then deletes '2', the remaining should be reindexed to 0,1,2.

3 Answers 3

2

There were some issues and improvements I made with your code available in this FIDDLE. Important: your system duplicates IDs, which is no good. I've switched the selectors to classes. I've added a reindex function for you.

HTML

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
    <p>
        <label for="p_scnts">
            <input type="text" class="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
        </label>
    </p>
</div>

JS

$(function () {
    function reIndex() {
        $('.p_scnt').each(function (k, v) {
            $(this).attr('name', 'p_scnt_' + k);
        });
    }
    reIndex();
    $('#addScnt').click(function () {
        var i = $('.p_scnt').length + 1;
        $('<p><label for="p_scnts"><input type="text" class="p_scnt" size="20" name="p_scnt_' + i + '" value="" placeholder="Input Value" /></label> <a href="#" class="remScnt">Remove</a></p>').appendTo($('#p_scents'));
        reIndex();
        $('.remScnt').click(function () {

            if (i > 1) {
                $(this).parents('p').remove();
                reIndex();
                i--;
            }
            return false;
        });
    });
});

Also, if you want to start number at 1 instead of 0 for the names, just modify this line:

$(this).attr('name', 'p_scnt_' + (k+1));
Sign up to request clarification or add additional context in comments.

1 Comment

If this is the solution, do not forget to accept it.
0

May be after deleting the one of the fields you could change de name attribute of all the remaining. I found something that could be useful for you in this link: Jquery change name attribute

Comments

0

By adding the following code you are changing the name attribute in each input (after removing the selected one):

$('#p_scents input[type="text"]').each(function(i){
    $(this).attr('name', 'p_scnt_' + (i + 1));
});

Modified html:

<h2>
    <a href="#" class="addScnt">Add Another Input Box</a>
</h2>

<div id="p_scents">
    <p>
        <label for="p_scnts">
            <input type="text" size="20" id="someid1" name="p_scnt" value="" placeholder="Input Value" />
        </label>
    </p>
</div>

Modified functions:

$('#addScnt').live('click', function() {
    $('<p><label for="p_scnts"><input type="text" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" id="someid_' + i + '" /></label> <a href="#" class="remScnt">Remove</a></p>').appendTo(scntDiv);

    i++;
    return false;
});

$('#remScnt').live('click', function() { 

    if( i > 2 ) {
        $(this).parents('p').remove();
        i--;

        $('#p_scents input[type="text"]').each(function(i){
            $(this).attr('name', 'p_scnt_' + (i + 1));
        });
    }

    return false;
});

Working demo: http://jsfiddle.net/tZPg4/9001/

I corrected one more thing. The id attribute must be unique by element, so every time you create a new element be sure you are adding the index at the end of the string (in your case).

1 Comment

Yes I am changing the ids to keep them unique. Although i haven't tested your code because Tomanow's answer worked for me. Thanks for the effort. :)

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.