Get all the values of a dropdownlist to an array using Javascript

how can I get the values of dropdownlist to an array?

Answers:

Answer
var ddlArray= new Array();
var ddl = document.getElementById('ddl');
for (i = 0; i < ddl.options.length; i++) {
   ddlArray[i] = ddl .options[i].value;
}

http://jsfiddle.net/2vtmP/

Answer

In pure Javascript you can iterate over the child nodes and pull out any nodes that have the nodeName option. Quick example:

var select = document.getElementById('whateverIdToYourSelect');

var arr = [];
for (var i = 0, l = select.childNodes.length; i < l; i++) {
    if (select.childNodes[i].nodeName === 'OPTION') arr.push(select.childNodes[i].innerHTML);
}
alert(arr) // [Contents,Of,Each,Option]
Answer
var sel = document.getElementById("yourSelectId");
var opts = sel.options;
var array = new Array();
for(i = 0; i < opts.length; i++)
{
    array.push(opts[i].value);
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.