Assuming I have a simple XML with only one item…
<myXML>
<valuesItem
name = 'name'
age = 'age'
gender = 'gender'
/>
</myXML>
…and want to import only the item’s attribute values to be the new values of a jQuery object like this…
var myObject = {
name: 'name',
age: 'age',
gender: 'gender'
};
…how shoud I proceed ???
I tried to achieve my purpose like this but I had no success so far:
var myObject = {};
$.get(myXML.xml, function(xmlData) {
var xmlValues = {};
$(xmlData).find('valuesItem').each(function() {
var $item = $(this);
xml Values = {
name: $item.attr('name'),
age: $item.attr('age'),
gender: $item.attr('gender')
};
});
$.extend(myObject, xmlValues);
});
How can I make public the $.get() return results ?
The problem here is that you are making an ajax call that is asynchronous. Either you use $.ajax and set the call not to be asynch or if you have to use the xml data, you must call a function in the success function of the $.get call.
for example you could do: