Skip to content

Instantly share code, notes, and snippets.

@tamizhvendan
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save tamizhvendan/7fd57f06e15b77c58d49 to your computer and use it in GitHub Desktop.

Select an option

Save tamizhvendan/7fd57f06e15b77c58d49 to your computer and use it in GitHub Desktop.
DropDownList manipulation using jQuery
$("#btnIn").click(function () {
$("#countries").val('IN');
});
$("#btnEng").click(function () {
$("#countries").val('ENG');
});
$("#btnAus").click(function () {
$("#countries").val('AUS');
});
$(document).ready(function () {
function loadDropDownList(collection) {
$.each(collection, function (index, value) {
var listItem = $("<option></option>").val(index).html(value);
$("#countries").append(listItem);
});
}
var myCollection = {'IN': 'India','AUS': 'Australia','ENG': 'England'};
loadDropDownList(myCollection);
})
$(document).ready(function () {
$("#countries").change(function () {
// Place your code here
});
});
Countries: <select id="countries"></select>
$(document).ready(function () {
$("#countries").change(function () {
var selectedValue = $("#countries").val();
$("#countryValue").text(selectedValue);
$("#countryText").text($("#countries > option[value='" + selectedValue + "']").html());
});
});
Selected Country’s Text: <span id="countryText"></span> <br />
Selected Country’s Value: <span id="countryValue"></span><br />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment