0

Possible Duplicate:
JavaScript - populate drop down list with array

I have following select box,

<select name="supplier" id="supplier"><option></option></select>

I want to set the values of this select box using Javascript. The Javascript variable may be a String or an Array. That means if its a String then there will be only one option in select box. And if it is an array the select box should contain multiple options. Someone please help me find the solution. Thanks in advance....

I have tried

document.getElementById('supplier').value="Option 1";

but its not working

Community
  • 1
  • 1
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
  • Do you want to set the options or the value? There is a difference. The value is the selected option(s). Also, were exactly are you stuck? – Felix Kling Oct 19 '12 at 05:33
  • What have you tried? This looks like you are asking someone to do your work more than an issue with something not working. – Travis J Oct 19 '12 at 05:35
  • I have edited my question, please undo the downvotes because my reputation is too low...And thanks for trying to help me. – Basim Sherif Oct 19 '12 at 05:38

5 Answers5

1

I would recommend to use jQuery, if you are doing a lot of DOM manipulation:

$("#supplier").append('<option value=1>My option</option>');

Without jQuery:

var select = document.getElementById("supplier");
select.options[select.options.length] = new Option('Text 1', 'Value1');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Soojoo
  • 2,146
  • 1
  • 30
  • 56
1

Try this:

http://jsfiddle.net/f4yEt/

function addOptions(input) {
    var sel = document.getElementById("supplier");
    if (typeof input === "string") {
        input = [input];
    }
    sel.options.length = 0;
    for (var i = 0; i < input.length; i++) {
        sel.options[sel.options.length] = new Option(input[i], input[i]);
        // This APPENDS options
    }
}

The input parameter can be a string or array (technically it only checks for string, to turn it into an array for iterating), and then dynamically appends an option for each item in the array.

Ian
  • 50,146
  • 13
  • 101
  • 111
1

To do this, you will need to create the nodes inside the .

var select = document.getElementById('supplier');
var option = document.createElement('option');
select.add(option);

Here is a larger tutorial that takes IE/FF differences into account: http://www.mredkj.com/tutorials/tutorial005.html

BUT

I would consider using jquery for this. Sujathan's answer has the jquery.

willoller
  • 7,106
  • 1
  • 35
  • 63
1

jQuery method for arrays

var array = ["item 1", "item 2", "item 3"];
$.each(array, function(i, value) {
  $('#supplier').append($(document.createElement('option')).attr('value', i + 1).text(value));
});​

http://jsfiddle.net/NN8KS/

Michael Theriot
  • 994
  • 7
  • 13
0
//var varOpt = "test";
var varOpt = new Array('test1', 'test2', 'test3');
var sel = document.getElementById('supplier');

if( typeof varOpt === 'string' ){
   sel.add(new Option(varOpt , varOpt));
} else {
    for(key in varOpt) {
        sel.add(new Option(varOpt[key], varOpt[key]));
    }
}