Reputation: 3246
my xml:
<stuffs>
<unit id="code">10</unit>
</stuffs>
my jquery ajax:
$.ajax({
type: "POST",
url: "xml/test.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('stuffs').each(function(){
... the code are here ...
});
}
i'm trying to get the values from <unit>
using the ID:
var unit = $(this).find('unit').attr('id');
var unitIdVal = $("#"+unit).text();
alert(unitIdVal);
but, nothing he found.
i dont know if is the same method of a comun jquery stuff's because i try to get the value direct using
var unitIdVal = $("#code").text();
but, nothing again.
ty
Upvotes: 0
Views: 3398
Reputation: 12730
Your selector stuff above is wonky to say the least. You're getting the ID off of an element and then getting that element once again by ID, all within a loop that does nothing for you...
Here's what it needs to be:
var unit = $(this).find('unit').attr('id');
var unitIdVal = $("#"+unit, xml).text(); //provide context to your selector
I feel dirty even writing this though, and I hope that it'll be used in a different context and this is merely illustrative.
Here's what it should be. Note the lack of selecting the very same element that you got the ID off of:
//....
success: function(xml) {
var unitIdVal = $(xml).find('unit').text();
}
//....
Please read some helpful tutorials on XML parsing thoroughly.
Upvotes: 1
Reputation: 1719
The problem is that $("#code").text();
looks for the id in the html document.
$("#code", xmlDoc).text();
should work.
Upvotes: 1