56

How can I dynamically set the options from my html select field with Javascript? This is my page setup:

<form name="test">
  <table>
    <tr>
      <td class='dataleft'>Product: </td>
      <td><select name='inptProduct'></select></td>
    </tr>
  </table>
</form>

I have all values in an array. This is the location to set the <option>s.

for(var i = 0; i < productArray.length; i++) {
    console.log("<option value='" + productArray[i] + "'>" + productArray[i] + "</option>");
}

PS: jQuery can be used.


Solution: This is the final solution I was looking for. It does not apply the new options to the select field. It removes all first, and then adds the new ones:

var optionsAsString = "";
for(var i = 0; i < productArray.length; i++) {
    optionsAsString += "<option value='" + productArray[i] + "'>" + productArray[i] + "</option>";
}
$("select[name='inptProduct']").find('option').remove().end().append($(optionsAsString));
nimrod
  • 5,595
  • 29
  • 85
  • 149
  • 4
    http://stackoverflow.com/questions/170986/what-is-the-best-way-to-add-options-to-a-select-from-an-array-with-jquery –  Apr 20 '12 at 13:41
  • http://stackoverflow.com/questions/47824/how-do-you-remove-all-the-options-of-a-select-box-and-then-add-one-option-and-se – AbiusX Feb 20 '15 at 01:27

8 Answers8

47

wellyou have almost done it all:

var optionsAsString = "";
for(var i = 0; i < productArray.length; i++) {
    optionsAsString += "<option value='" + productArray[i] + "'>" + productArray[i] + "</option>";
}
$( 'select[name="inptProduct"]' ).append( optionsAsString );

EDIT removed $ wrapper around last optionsAsString as append automatically converts strings

gotofritz
  • 3,341
  • 1
  • 31
  • 47
27
var $inputProduct = $("select[name='inputProduct']")

$(productArray).each(function(i, v){ 
    $inputProduct.append($("<option>", { value: v, html: v }));
});
hunter
  • 62,308
  • 19
  • 113
  • 113
6

Assuming that the array productArray is a simple array, with each element being the value of the option, and the desired title for that option.

$( 'select[name="inptProduct"]' )
  .html( '<option>' + productArray.join( '</option><option>' ) + '</option>' );

Example:

productArray = ['One', 'Two', 'Three'];

// With the original code being

<select name="inptProduct">
  <option>There could be no options here</option>
  <option>Otherwise, any options here will be overwritten</option>
</select>

// Running the code above would output

<select name="inptProduct">
  <option>One</option>
  <option>Two</option>
  <option>Three</option>
</select>
Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41
3
function onLoad() {

  var type = new Array("Saab","Volvo","BMW");

    var $select = $("select[name='XXXXXType']")
    alert($select);
    $(type).each(function(i, v){
        $select.append($("<option>", { value: v, html: v }));
      });

}
<select name="XXXXXType" id="XXXXXType"></select>
Matt
  • 74,352
  • 26
  • 153
  • 180
2

Here is a solution for people who don't want to use jQuery:

const select = document.getElementById('select')

const arr = ['Apple', 'Pear', 'Orange']

arr.forEach(value => {
  const option = document.createElement('option')
  option.innerHTML = value
  select.appendChild(option)
})
<select id="select"></select>
programmerRaj
  • 1,810
  • 2
  • 9
  • 19
1

If you not insisting for pure JavaScript to do the job, you can easily done the job using jQuery.

<form name="test">
  <table>
    <tr>
      <td class='dataleft'>Product: </td>
      <td><select id='inptProduct'></select></td>
    </tr>
  </table>
</form>


for (var i = 0; i < productArray.length; i++) {
    $("#inptProduct").append('<option value=' + if_you_want_set_value + '>' + productArray[i] + '</option>');
    }

Note that here I used the id of select tag. That's why I add the html part too. Hope you know how to add jQuery.

Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
0

I usually do it like this:

document.getElementById("selectid").innerHTML="<option value='foo'>FOO</option>";

Radacina
  • 431
  • 5
  • 10
-5

you can set id for the select id=inptProduct Then do $('#inptProduct').val(yourselectValue)

ZERO
  • 133
  • 3