Add options to a drop-down list using jQuery.
HTML Code :
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-git.js"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Add options to a drop-down list using jQuery.</title> </head> <body> <p>List of Colors :</p> <select id='myColors'> <option value="Red">Red</option> <option value="Green">Green</option> <option value="White">White</option> <option value="Black">Black</option> </select> </body> </html>
JavaScript Code:
var myOptions = { val1 : 'Blue', val2 : 'Orange' }; var mySelect = $('#myColors'); $.each(myOptions, function(val, text) { mySelect.append( $('<option></option>').val(val).html(text) ); });
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
HTML Code :
JavaScript Code: