Skip to content

Instantly share code, notes, and snippets.

@umidjons
Last active December 20, 2015 13:29
Show Gist options
  • Select an option

  • Save umidjons/6139693 to your computer and use it in GitHub Desktop.

Select an option

Save umidjons/6139693 to your computer and use it in GitHub Desktop.
Dynamically create <select> and populate <option>s with jQuery.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Select tag</title>
</head>
<body>
<script src="jquery-1.10.2.js"></script>
<script>
jQuery(document).ready(function($){
var select = $('<select>',{
id: 'my-select',
class: 'sel-box',
multiple: true
}).appendTo('body');
$.each({'key1':'val1','key2':'val2','key3':'val3'}, function(key,val){
select.append($('<option>',{
value: key,
text: val
}));
});
});
</script>
</body>
</html>
<select id="my-select" class="sel-box" multiple="multiple">
<option value="key1">val1</option>
<option value="key2">val2</option>
<option value="key3">val3</option>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment