0

I'm making a form in which one field set will have input fields added by the user as necessary that will have a selectable height and an optional check-box. I found a good example on how to add one input type at a time but not both simultaneously and paired together. Any ideas would be appreciated on this one! Thanks! Y'all are my hero. The example for a single input code can be found here http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding-form-elements/

HTML:

<form id="myForm">
    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
        Name: <input type="text" name="name1" id="name1" />
    </div>

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

JS:

$(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
                var newNum  = new Number(num + 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 newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

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

                // insert the new element after the last "duplicatable" input field
                $('#input' + num).after(newElem);

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

                // business rule: you can only add 5 names
                if (newNum == 5)
                    $('#btnAdd').attr('disabled','disabled');
            });

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

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

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

            $('#btnDel').attr('disabled','disabled');
        });
1
  • what do you mean by selectable height? Commented Jan 6, 2014 at 23:29

1 Answer 1

3

The code you have will already clone what ever is inside of the .clonedInput div. So all you need to do is add a checkbox after the textbox.

<div id="input1" style="margin-bottom:4px;" class="clonedInput">
    Name: <input type="text" name="name1" id="name1" /> <input type="checkbox" name="chk1" id="chk1" />
</div>

To get the id/name values to append a new number you'll need to adjust this line, which will give the textbox elements an ID and Name of name1, name2, name3, etc.:

newElem.children('input[type=text]:first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);

and add this line, which will give the checkbox elements an ID and Name of chk1, chk2, chk3, etc.:

newElem.children('input[type=checkbox]:first').attr('id', 'chk' + newNum).attr('name', 'chk' + newNum);

Here is an updated fiddle.

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

2 Comments

Okay I looked at the JSFiddle before I had to head out and now that I'm back it won't activate the "remove name" button. No idea what just happened. I even went back to the /1/ version to make sure that it was the original version. Is it still working on your end Goose? Also, AWESOME username.
Here is another fiddle, I'm not sure if it is the version of jQuery change, or perhaps a browser issue but I changed the lines that enable the buttons from $('#btnDel').attr('disabled',''); to $('#btnDel').removeAttr('disabled');. That should do the trick!

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.