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>
.after().html(str)doesn't make sense. What it really does is set the html of the div tostr,.afteris skipped over due to it having no html content to append after the element.