Reputation: 213
I'm using jquery to populate a dropdown box. Here is my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<script type="text/javascript" src="jquery.min.js"></script>
<title>Check</title>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "modules.xml",
dataType: "xml",
success: function(xml) {
var select = $('#mySelect');
$(xml).find('modules').each(function(){
$(this).find('MAC').each(function(){
var value = $(this).text();
select.append("<option class='ddindent' value='"+ value +"'>"+value+"</option>");
});
});
select.children(":first").text("please make a selection").attr("selected",true);
}
});
});
</script>
</head>
<body>
<div id="page-wrap">
<h1>Test</h1>
<form action="test.asp" onsubmit="" method="post" >
<select name="mySelect" id="mySelect">
<option>loading</option>
</select>
<input type="submit" value="Change">
</form>
</div>
</body>
</html>
I would like to give each dropdown item a value corresponding to its position in the xml file, but i have no idea of how to do this. The reason i would like to make this is to be able to delete the item from the xml. Does anyone has any idea on how to do this?
Upvotes: 0
Views: 228
Reputation: 92983
http://api.jquery.com/each/ can use an index argument in the function:
$(xml).find('modules').each(function(module_idx){
$(this).find('MAC').each(function(mac_idx){
With that, you can locate it in the XML file with something like $(xml).find('modules').eq(module_idx).find('MAC').eq(mac_idx)
Upvotes: 1