1

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>
1
  • 1
    .after().html(str) doesn't make sense. What it really does is set the html of the div to str, .after is skipped over due to it having no html content to append after the element. Commented Dec 10, 2012 at 21:13

2 Answers 2

2

You are using .after() wrong.. If you want the element to after your selected element then do this

newIngredientTextBox.after('<input type="text" name="ingredient' + counter + 
          '" id="ingredient' + counter + '" value="#' + counter + '" />');

If you want it to be inside div element then you can use .append()

newIngredientTextBox.append('<input type="text" name="ingredient' + counter + 
          '" id="ingredient' + counter + '" value="#' + counter + '" />');

Also you need to append the element after you add it to the dom.. not before

newIngredientTextBox.appendTo("#allIngredientsDiv").after('<input type="text" name="ingredient' + counter + 
          '" id="ingredient' + counter + '" value="#' + counter + '" />');
Sign up to request clarification or add additional context in comments.

2 Comments

my understanding was it was to be after the div :/?
It could have been either actually.. as he used .after() - next and .html() - inside
0

Try

var counter = 2;
var newIngredientTextBox = $(document.createElement('div')).attr("id", 'div_ingredient' + counter);         
console.info(newIngredientTextBox);
newIngredientTextBox.after('<input type="text" name="ingredient' + counter +  '" id="ingredient' + counter + '" value="#' + counter + '" >');

after then added html won't work

1 Comment

Hmmm.. Thanks for your response, @JamieHutber. Still no luck, it seems that either the DOM isn't being refreshed, or I'm missing something that's causing the browser to simply ignore the code. Am I right in thinking the premise of the code is solid? I smell something stupid that I'm doing and can't for the life of me figure out what.

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.