tags:

views:

54

answers:

2

I have the following code which is loading an xml file to populate a dropdown menu. Right now the value equals the option text but I would like to have the value equal to some number which comes from the same xml file. Can someone tell me how to format the xml file to do this and what code to add to make the value appear in the html code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
<title>Using jQuery and XML to populate a drop-down box demo</title>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "make.xml",
dataType: "xml",
success: function(xml) {
var select = $('#mySelect');

$(xml).find('dropdown').each(function(){
$(this).find('make').each(function(){
var value = $(this).text();
select.append("<option class='ddindent' value='"+ value +"'>"+value+"</option>");
});
});
select.children(":first").text("Select Make").attr("selected",true);
} //sucess close
}); 
}); 
</script>
</head>
<body>
<div id="page-wrap">
<select id="mySelect">
<option>loading</option>
</select>
</div>
</body>
</html>

This is the xml file

<?xml version="1.0" encoding="iso-8859-1"?>

<dropdown>
<make>Acura</make>
<make>Aston Martin</make>
<make>Audi</make>
<make>Bently</make>
<make>BMW</make>
<make>Buick</make>
<make>Cadillac</make>
<make>Chevrolet</make>
<make>Chrylser</make>
<make>Dodge</make>
<make>Eagle</make>
<make>Ferrari</make>
<make>Ford</make>
<make>Geo</make>
<make>GMC</make>
<make>Honda</make>
<make>Hummer</make>
<make>Hyundai</make>
<make>Infiniti</make>
<make>Isuzu</make>
<make>Jaguar</make>
<make>Jeep</make>
</dropdown>  
A: 

Not sure really what you are asking, but you could do 1 of two things.

Have a count variable in the javascript and everytime you add an option increment this variable. Use that number as the "value" and it will correspond to its "point" in the XML.

Or in the xml do each "make" like this:

<make>
    <id>1</id>
    <name>Ford</name>
</make>

and use the ID.

Not sure what you really want though, so don't know if this will help.

Thomas Clayson
Well I thought of that but the values are not sequential for instance Dodge can have a value of 300 while Audi can have a value of 13 so it had to pull the value from the xml file for each make
I'm still not sure what you are trying to do. Are you constantly updating and editing the XML yourself?
Thomas Clayson
+3  A: 

First, start by putting the numbers in your xml file. If they are related to the dropdown item, i suggest as an attribute.

example:

<dropdown>
    <make value="1">Acura</make>
    <make value="4">Aston Martin</make>
    <make value="34">Audi</make>
...
</dropdown>

then in your jquery loop:

$(xml).find('dropdown').each(function(){
     $(this).find('make').each(function(){
          var value = $(this).attr('value');
          var label = $(this).text();
          select.append("<option class='ddindent' value='"+ value +"'>"+label+"</option>");
     });
});

Note that you could probably simplify and speed up your jquery like this (untested):

$('make', xml).each(function(){
              var value = $(this).attr('value');
              var label = $(this).text();
              select.append("<option class='ddindent' value='"+ value +"'>"+label+"</option>");
    });

UPDATE

For another, important, performance boost, do only one append() instead of as many append() as you have "make" nodes.

var optionsHtml = new Array();
    $('make', xml).each(function(){
                  var value = $(this).attr('value');
                  var label = $(this).text();
optionsHtml.push( "<option class='ddindent' value='"+ value +"'>"+label+"</option>");

        });
optionsHtml = optionsHtml.join('');
select.append(optionsHtml);
pixeline
That was very simple and worked great!!! Thx!!!
my pleasure. There is an additional performance trick: you are appending a node on every each() iteration. Since you have many nodes, it's quite costly in terms of performance. It's advised that you store the html in an array, and after the each, join the array and do the append.
pixeline
updated answer with an implementation of this performance boost.
pixeline
Yes I can see how that would be better. Instead of lets say in this case 22 appends (one for each loop) there would be only one. So my understanding of this is that each time it "grabs" the node it puts it in the array and then after the loops are finished it then takes the complete array and then appends it. Outstanding!!!
Only thing is I cannot get the updated code to work, perhaps I am putting in in the wrong place? Syntax error on this line optionsHtml[] = "<option class='ddindent' value='"+ value +"'>"+label+"</option>";
my mistake: i mixed up php and javascript syntax. optionsHtml.push('<option>...'); should work better. The array declaration should also be like this: var x = new Array();
pixeline
Worked great thanks for the education. I have another question but I will post a new question. THX again!!