Reputation: 356
I am creating a dynamic form in which the user can add elements dynamically. I am able to allow them to add text boxes however I'm not sure how to add my drop down menu into it. Here is my jQuery code
var addDiv2 = $('#addIngredient');
var i = $('#addIngredient p').size() + 1;
$('#addNewIngredient').on('click', function () {
$('<p> <input id="ingredient_' + i + '" name="ingredient[]" type=text"
value="" placeholder="Ingredient" />
<input id="quantity_' + i + '"
name="quantity[]" type=text" value="" placeholder="Quantity" />
<input id=alternative_' + i + '" name="alternative[]" type=text"
value"" placeholder="Alternative Ingredient" />
<a href=#" class="remNew2"> Remove </a> </p> ').appendTo(addDiv2);
Here is my html
<div id="addIngredient">
<p>
<input type="text" id="ingredient_1" name="ingredient[]" value=""
placeholder="Ingredient"/>
<input type="text" id="quantity_1" name="quantity[]" value=""
placeholder="Quantity"/>
<input type="text" id="alternative_1" name="alternative[]" value=""
placeholder="Alternative Ingredient"/>
<select id="selectQuantity_1" name="quantityType[]">
<option value="grams">Grams</option>
<option value="ounces">Ounces</option>
<option value="Pounds">Pounds</option>
</select>
<a href="#" id="addNewIngredient">Add</a>
I have tried but can't work it out, help would be appreciated!
Here is the jsFiddle http://jsfiddle.net/3yFFr/
ignore the bit below the jQuery i pasted, i had to paste it all in for it to work.
Upvotes: 0
Views: 199
Reputation: 442
In your code, you didn't add the select menu to what you appended with the jQuery. Look:
$('<p> <input id="ingredient_' + i + '" name="ingredient[]" type=text" value=""
placeholder="Ingredient" />
<input id="quantity_' + i + '" name="quantity[]" type=text" value=""
placeholder="Quantity" />
<input id=alternative_' + i + '" name="alternative[]"
type=text" value"" placeholder="Alternative Ingredient" />
<a href=#" class="remNew2"> Remove </a> </p> ').appendTo(addDiv2);
Upvotes: 0
Reputation: 1010
If you want to add a row on each "Add" click, then I would try something like this:
var addDiv2 = $('#addIngredient');
var formRow = addDiv2.find('p');
$('#addNewIngredient').on('click', function () {
formRow.clone(true, true).appendTo(addDiv2);
});
See this jsFiddle for a working example.
EDIT : Since you need to keep your ids in check, I rigged up a little something that should work,
var addDiv2 = $('#addIngredient');
$('#addNewIngredient').on('click', function () {
// Clone the last input row, keeping the event handlers
var clonedRow = $('#addIngredient p').last().clone(true, true);
// We're going to examine all select and input elements
var all = clonedRow.find('select, input');
// A regular expression we can use to test if the id has numbers at the end
var numericPostfix = /\d+$/;
for (var i = 0; i < all.length; i++) {
var curr = $(all[i]);
// If current element has an id attribute
if (curr.attr('id') != undefined) {
var id = curr.attr('id');
// Rips off any trailing digits in element's id
var idNum = numericPostfix.exec(id);
// Exec gives an array of matches, if it's not null, choose the first match
if (idNum) {
idNum = idNum[0];
}
if (idNum) {
// Chop off last character
id = id.slice(0, (-1 * idNum.length));
// Increment and add the id number to the nextId
var nextId = id + (Number(idNum) + 1);
// Set the id attribute to nextId
curr.attr('id', nextId);
}
// Append to the DOM
clonedRow.appendTo(addDiv2);
}
}
});
Upvotes: 0
Reputation: 56501
It seems you're cloning the elements within the div. I would suggest you to go ahead and use .clone()
method to do this.
See pretty simplified code. Now here you can remove the add element and change it to remove.
$('#addIngredient').clone()
.prop('id', 'addIngredient1').appendTo('ingredDiv');
And also try to avoid using id
unless it is necessary.
Upvotes: 1