Convert html select box into javascript array
by MANIMUTHUPANDI[ Edit ] 2013-10-19 18:33:29
You can convert the html select box to javascript array using the following script.
This also will be used to add dynamic options into the select box.
Here i have given the javascript code to view the output..
function showSelectbox_array()
{
selectlength=$('#fruits option').length;
var group1=$('#fruits option');
var HTMLarray=new Array();
for(cc=0;cc<selectlength;cc++)
{
//alert(group1.val())
HTMLarray.push(group1.val());
group1=group1.next();
}
alert(HTMLarray);
}
function addtoSelectbox()
{
groupname2=prompt("Enter the new category name","")
selectlength=$('#fruits option').length;
var group1=$('#fruits option');
var HTMLarray=new Array();
var allow2="no";
for(cc=0;cc<selectlength;cc++)
{
if(group1.val()==groupname2)
{allow2="no";break;}
else
allow2="yes";
group1=group1.next();
}
if(allow2=="yes" && groupname2!=null && groupname2!="")
$('#fruits').append('<option value='+groupname2+' selected>'+groupname2+'</option>');
}