Skip to content

Instantly share code, notes, and snippets.

@CheapDevotion
Created September 4, 2012 16:54
Show Gist options
  • Select an option

  • Save CheapDevotion/3623406 to your computer and use it in GitHub Desktop.

Select an option

Save CheapDevotion/3623406 to your computer and use it in GitHub Desktop.
Converts an html select list to a javascript object.
var array = new Object();
$("#select option").each(function() {
var optgroup = $(this).parent().attr("label");
if (optgroup != undefined) {
if (array[optgroup] == undefined) {
array[optgroup] = new Object();
}
array[optgroup][$(this).val()] = $(this).text();
}
else {
array[$(this).val()] = $(this).text();
}
});
console.log(JSON.stringify(array));​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment