Sure! Here's an example of jQuery code that will dynamically display the data from the given array into a dropdown:
HTML:
HTML:
<select id="myDropdown"></select>
jQuery:
JavaScript:
$(document).ready(function() {
var data = {
'One': '1',
'Two': '2',
};
var dropdown = $('#myDropdown');
$.each(data, function(key, value) {
dropdown.append($('<option></option>').attr('value', value).text(key));
});
});
Explanation:
1. The
data
variable contains the array you provided, where the keys represent the display values and the values represent the option values.
2. The
dropdown
variable selects the dropdown element using its ID.
3. The
$.each()
function iterates over each key-value pair in the
data
array.
4. For each pair, a new
option
element is created using the
text()
and
attr()
functions to set the display value and option value accordingly.
5. Finally, the
append()
function adds the newly created
option
element to the dropdown.
Make sure to include the jQuery library in your HTML file for this code to work properly.