I have 4 blocks of xml, that I want to parse whith jQuery making one ajax call. Now four blocks of xml are different from each other(But the structure is same). I am trying to parse this xml by passing an id to each of the xml block, But in my ajax call I am not able to retreive the id attribute as it is the first thing that I need to capture to pass to the ajax function.
I tried something like this:
XML is here:
<dropdown>
<test id="1">
<optionheading>
<heads>Heading 1</heads>
<value>
<values images='images/1.gif'>Option1</values>
<values images='images/2.gif'>Option2</values>
<values images='images/3.gif'>Option3</values>
</value>
</optionheading>
.....................................
............................
</test>
<test id='2">
<optionheading>
<heads id="b">H..................
..................
............
</test>
</dropdown>
I need to get id of test1, test2, et-al
Ajax is Here:
$("document").ready(function() {
$.ajax({
type: "GET",
url:"dropdown.xml",
dataType: "xml",
success :function(xml) {
var select = $("#mySelect");
//I am getting id here
var id=$(xml).find('test').attr('id');
$(xml).find('test'+id).each(function() {
$(this).find('optionheading').each(function() {
var opthead = $(this).fin......................
If I’ve understood you well, the following should happen (output boldfaced):
<test id="IDHERE">should populate<select ID="IDHERE"><optionheading>should create an<optgroup>with a label as defined at<heads><optgroup>is populated with<option>elements, according to the given<values>elementsThe code below is the jQuery method to implement this idea, as requested.
Note:
$('test', xml)is a shorthand for$(xml).find('test').Note2: Since an ID must be unique,
select#yourIDis equal to#yourID.Note3: The
<values>tag at the XML is not necessary for this script.Possible HTML after executing: