Skip to content

Instantly share code, notes, and snippets.

@govaniso
Created October 8, 2012 17:08
Show Gist options
  • Save govaniso/3853653 to your computer and use it in GitHub Desktop.
Save govaniso/3853653 to your computer and use it in GitHub Desktop.
<form id="example-form">
<select id="exampleSelect">
<option selected>Primera opcion</option>
<option>Segunda opcion</option>
<option>Ttercera opcion</option>
<option>Otra mas</option>
<option>La ultima.</option>
</select>
<div>
</div>
<input onclick="$('#exampleSelect option').remove()" value="Clear options" type="button">
<input onclick="exampleAddOption1()" value="Add option" type="button">
<input onclick="exampleAddOption2()" value="Replace options" type="button">
<input onclick="$('#exampleSelect').html(exampleBackup)" value="Reset" type="reset">
</form>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://twitter.github.com/bootstrap/js/bootstrap.min.js"></script>
<script language="Javascript">
exampleBackup = $('#example').html();
function exampleAddOption1() {
var index = $('#exampleSelect option').length + 1;
$('#exampleSelect').append('<option value="example' + index + '" selected="selected" Opcion #' + index + '</option>');
}
function exampleAddOption2() {
var newOptions = {
'rojo' : 'Rojo',
'azul' : 'Azul',
'verde' : 'Verde',
'rosa' : 'Rosa'
};
var selectedOption = 'verde';
var select = $('#exampleSelect');
var options = select.attr('options');
$('option', select).remove();
for(var index in newOptions) {
options[options.length] = new Option(newOptions[index], index);
}
select.val(selectedOption);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment