Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  147] [ 4]  / answers: 1 / hits: 5487  / 11 Years ago, mon, january 6, 2014, 12:00:00

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');
});

More From » jquery

 Answers
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.


[#48943] Monday, January 6, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 2 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;