Skip to content

Instantly share code, notes, and snippets.

@ThomasGaubert
Created December 28, 2012 04:49
Show Gist options
  • Save ThomasGaubert/4394605 to your computer and use it in GitHub Desktop.
Save ThomasGaubert/4394605 to your computer and use it in GitHub Desktop.
Dropdown selection changer
var $options = $("#options");
var options = [
{id: "0", value: "Select one..."},
{id: "1", value: "Choice 1"},
{id: "2", value: "Choice 2"},
{id: "3", value: "Choice 3"},
{id: "4", value: "Choice 4"}
];
function getOptions() {
var optionId = $options.val();
if(optionId == 0) {
document.getElementById("result").innerHTML='';
}
if(optionId == 1) {
document.getElementById("result").innerHTML='You chose option 1';
}
if(optionId == 2) {
document.getElementById("result").innerHTML='You chose option 2';
}
if(optionId == 3) {
document.getElementById("result").innerHTML='You chose option 3';
}
if(optionId == 4) {
document.getElementById("result").innerHTML='You chose option 4';
}
}
for (i = 0; i < options.length; i++) {
$options.append("<option value='" + options[i].id + "'>" + options[i].value + "</option>");
}
getOptions();
$options.on("change", function(){
getOptions();
});
<html>
<body>
<select id="options"></select>
<br>
<div id="result"></div>
</body>
<script src="jquery.js"></script>
<script src="changer.js"></script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment