2

I'm trying to add more inputs using a button using JS and can't figure it out.

So, I have this code in my HTML:

 <div id="ingredients" class="ingredients">
      <h2 class="tk-freight-sans-pro">Ingredients</h2>
      <input type="text" placeholder="Ingredient">
      <input type="text" placeholder="Ingredient">
      <input type="text" placeholder="Ingredient">
      <br>
      <button class="button" onclick="addIngredient();">Add Ingredient</button>
    </div>

And my JS is here:

function addIngredient() {
        var list = document.getElementById("ingredients")
        list.innerHTML += "<input type="text" placeholder="Ingredient">"
     }

Does anyone know what's going on or what I need to do? Thanks!

1 Answer 1

3

This:

list.innerHTML += "<input type="text" placeholder="Ingredient">"

should be like that:

list.innerHTML += "<input type=\"text\" placeholder=\"Ingredient\">"

or like that:

list.innerHTML += '<input type="text" placeholder="Ingredient">'

You can't use a double quote in a string wrapped by double quotes. You have to escape the quotes in the string or use simple quotes to wrap it.

Also you should put a ; after each instruction.

Sign up to request clarification or add additional context in comments.

1 Comment

@tns12 - You should accept this answer as correct :) (assuming it solved the problem you asked about, which it should)

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.