I’ve been on this for more than a half a day and I can’t figure out what I’m missing. I’ve tried many different iterations and every other solution I could find. It should be a no-brainer. I am using JQueryMobile 1.2 and it’s dependencies Here’s the javascript (note var counter = 2; is declared above the code:
$("#btn_addIngredient").click(function () {
if (counter > 10) {
alert("Please limit to 10 ingredients");
return false;
}
var newIngredientTextBox = $(document.createElement('div')).attr("id", 'div_ingredient' + counter);
newIngredientTextBox.after().html('<input type="text" name="ingredient' + counter +
'" id="ingredient' + counter + '" value="#' + counter + '" >');
newIngredientTextBox.appendTo("#allIngredientsDiv");
counter++;
}); //~ $("#btn_addIngredient")
And the html:
<div id="IngredientsTextGroup" data-role="fieldcontain">
<fieldset id="IngredientsFieldset" data-role="controlgroup" data-mini="true">
<div id="allIngredientsDiv">
Ingredients
<div id="div_ingredient1">
<input id="ingredient1" value="#1" type="text">
</div>
<!-- *** Need to add <div_ingredient2, 3 ...> -->
</div>
<a id="btn_addIngredient" href="#" data-role="button" data-inline="true" data-icon="plus"
data-iconpos="left">
Add Ingredient
</a>
<div>
You are using .after() wrong.. If you want the element to after your selected element then do this
If you want it to be inside div element then you can use .append()
Also you need to append the element after you add it to the dom.. not before