Skip to content

Instantly share code, notes, and snippets.

@sebby
Created July 14, 2014 07:16
Show Gist options
  • Select an option

  • Save sebby/08a412bdc717c556552e to your computer and use it in GitHub Desktop.

Select an option

Save sebby/08a412bdc717c556552e to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[add your bin description]" />
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<select id="select1" multiple>
<option value="1">valeur 1</option>
<option value="2">valeur 2</option>
<option value="3" selected>valeur 3</option>
<option value="4" selected>valeur 4</option>
<option value="5">valeur 5</option>
</select>
</body>
</html>
// Return an array of the selected opion values
// select is an HTML select element
function getSelectValues(select) {
var result = [];
var options = select && select.options;
var opt;
for (var i=0, iLen=options.length; i<iLen; i++) {
opt = options[i];
if (opt.selected) {
result.push(opt.value || opt.text);
}
}
return result;
}
console.log(getSelectValues(document.getElementById('select1')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment