1. jquery.min.js
Finally, the loop is executed over all the
Customer nodes and inside the loop, the values of the
CustomerId and
Name child nodes is read and is used to create and add
HTML Option element to the
HTML DropDownList.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<input type="button" id="btnGenerate" value="Populate DropDownList" onclick="PopulateDropDownList()" />
<hr />
<select id="ddlCustomers">
</select>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function PopulateDropDownList() {
//Build an XML containing Customer records.
var xml = "<Customers>";
xml += "<Customer>";
xml += "<CustomerId>1</CustomerId><Name>John Hammond</Name><Country>United States</Country>";
xml += "</Customer>";
xml += "<Customer>";
xml += "<CustomerId>2</CustomerId><Name>Mudassar Khan</Name><Country>India</Country>";
xml += "</Customer>"
xml += "<Customer>";
xml += "<CustomerId>3</CustomerId><Name>Suzanne Mathews</Name><Country>France</Country>";
xml += "</Customer>";
xml += "<Customer>";
xml += "<CustomerId>4</CustomerId><Name>Robert Schidner</Name><Country>Russia</Country>";
xml += "</Customer>";
xml += "</Customers>";
var xmlDoc = $.parseXML(xml);
var customers = $(xmlDoc).find("Customer");
var ddl = $("#ddlCustomers");
customers.each(function () {
var option = $("<option />");
//Set Customer Name in Text part.
option.html($(this).find("Name").text());
//Set Customer CustomerId in Value part.
option.val($(this).find("CustomerId").text());
//Add the Option element to DropDownList.
ddl.append(option);
});
}
</script>
</body>
</html>
* All browser logos displayed above are property of their respective owners.