Last active
December 20, 2015 13:29
-
-
Save umidjons/6139693 to your computer and use it in GitHub Desktop.
Dynamically create <select> and populate <option>s with jQuery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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