0

I'm trying to add a dropdown menu and other input fields dynamically. So far it works fine adding Text input field, but it does nothing with Select input (dropdown).

This is the HTML:

<form method="POST"> 
    <div id="dynamicInput"></div>
    <input type="button" onClick="addInput('dynamicInput');" value="Add inputs"/>
</form>

This is the Javascript:

function addInput(divName){
    var newdiv = document.createElement('div');
    newdiv.innerHTML = 
   "<input type='text' id='name'>"+
   '<select id="age">
       <option value="18">18</option>
       <option value="18">18</option>
   </select>';
   document.getElementById(divName).appendChild(newdiv);
}

Peace.

2
  • are you missing literal quotes in the real copy or just on here? Commented Mar 17, 2015 at 23:57
  • @dandavis I've tried also this with no success: newdiv.innerHTML = "<input type='text' id='name'>"+ "<select id='age'> <option value='18'>18</option> <option value='18'>18</option> </select>"; Commented Mar 18, 2015 at 3:04

1 Answer 1

1

I bet your console tells you something like unexpected token. Concatenation of a multiline string requires escaping.

You can do:

myVar = "hello \
    sir!";

or concatenate each line:

myVar = "hello " + 
    "sir!";
Sign up to request clarification or add additional context in comments.

Comments

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.