4

I'm trying to populate a drop down menu with a javascript array. I can get individual elements to show up but not the entire array. I'm sure this question has been asked before, but can't find any references. Any help would be appreciated.

var sex=["male","female", "unknown"];

for (i=0;i<sex.length;i++){
var opt = document.createElement("option");
document.getElementById("m").innerHTML=sex[i];
}

The html is:

    <form name = "demo">
    <table id = "demo">
    <th>Demographics</th>
    <tr><td>Sex</td><td><select><option id="m"></option></select></td></tr>
    </table>
    </form>
3
  • The current code overwrites the list each iteration. Commented Oct 3, 2015 at 18:23
  • How would I fix that? Commented Oct 3, 2015 at 18:26
  • Have you tried using a for each cycle? Something like for( index in array). You could also try a for...of but is not compatible with IE Commented Oct 3, 2015 at 18:27

3 Answers 3

2

See below an non-elegant fix to your problem. You can refactor this to nicer looking code if you use the JQuery library, see for example What is the best way to add options to a select from an array with jQuery?

var sex = ["male", "female", "unknown"];

for (i = 0; i < sex.length; i++) {
  var opt = document.createElement("option");
  document.getElementById("m").innerHTML += '<option id="' + i + '">' + sex[i] + '</option>';
}
<form name="demo">
  <table id="demo">
    <th>Demographics</th>
    <tr>
      <td>Sex</td>
      <td>
        <select id="m">

        </select>
      </td>
    </tr>
  </table>
</form>

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

1 Comment

This works perfectly. It'll save me so much effort in writing redundant code. Thanks.
1

This is a method I typically use that works:

Codepen Demo

HTML:

<form name="demo">
  <table id="demo">
    <th>Demographics</th>
    <tr>
      <td>Sex</td>
      <td>
        <select id = "sex">

        </select>
      </td>
    </tr>
  </table>
</form>

Javascript:

//array to hold the persons sex
var sex = ["male", "female", "unknown"];

//array to store html to add to the select list
var html = [];

//loop through the array
for (var i = 0; i < sex.length; i++) {//begin for loop

  //add the option elements to the html array
  html.push("<option>" + sex[i] + "</option>")

}//end for loop

//add the option values to the select list with an id of sex
document.getElementById("sex").innerHTML = html.join("");

1 Comment

Try this again (didn't go through the first time)... this is perfect. Thank you all for your suggestions.
-1

Use a template libary like underscore or handlebars or mustache. Its bad practice to generate html from javascript.

2 Comments

Can you elaborate why you believe it is bad practive to generate html from JavaScript. Does underscore,handlebars, and mustache use JavaScript?
You can Google it, you will get lot's answers for it. stackoverflow.com/questions/1284381/…

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.