0

This is my jquery code to read "test.xml".

var htmldata = 0;
$.get('test.xml', function(data) {
    xml_data = $(data);
    xml_data.find("order").find("customer").each(function(k, v) {
        divClass = inactiveClass;
        spanClass = inactiveIcon;
        htmldata += '<div class="' + divClass + '"><span class="' + inactiveIcon + '"></span>' + $(this).text() + '</div>';
    });
});
alert(htmldata);

In this code I tried to parse test.xml and creating one htmldata. But I am not getting that data out side. If I tried to alert its showing null. How can I take that value outside. Please help me.

Adi
  • 5,089
  • 6
  • 33
  • 47
DEVOPS
  • 18,190
  • 34
  • 95
  • 118
  • 1
    I think that alert(htmldata ); will show 0 because the code execution doesn't wait for the request [which actually retrieves the data from test.xml] to finish. – Nick Jul 30 '12 at 06:50
  • As @OptimusPrime said, your ajax request is asynchronous. Put the alert inside the `$.get`'s function callback. – Fabrício Matté Jul 30 '12 at 06:51
  • @OptimusPrime, indeed, totally skipped me. Even though there was a question about yesterday. – Adi Jul 30 '12 at 06:52
  • Hi I created a separe function to parse that data. var xml_htmldata = $.myorder.parseXMLdata(uniqueId,options); alert(xml_htmldata); now also its giving undefind message. – DEVOPS Jul 30 '12 at 07:05
  • forget what I said (I'll remove it, sorry for confusing you) read @OptimusPrime's comment, this might offer you some explanation http://stackoverflow.com/questions/11688171/after-calling-chrome-tabs-query-the-results-are-not-available/11689804#11689804 – Adi Jul 30 '12 at 07:05
  • Anything that depends on the `htmldata` received from `$.get` will have to be wrapped inside a function which you call from the `$.get`'s callback (or put the code directly after parsing it, inside the callback itself), simple as that. – Fabrício Matté Jul 30 '12 at 07:08

1 Answers1

3
function parseXMLdata(data, callback)
{    
    $.get('test.xml', function(data) {
        xml_data = $(data);
        xml_data.find("order").find("customer").each(function(k, v) {
            divClass = inactiveClass;
            spanClass = inactiveIcon;
            var htmldata += '<div class="' + divClass + '"><span class="' + inactiveIcon + '"></span>' + $(this).text() + '</div>';

            callback(htmldata);    
        });
    });
}

parseXMLdata(data, window.alert);
Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
  • Hi how can I assign that alert value to one variable – DEVOPS Jul 30 '12 at 11:29
  • I need like this var stat = parseXMLdata(data, window.alert); – DEVOPS Jul 30 '12 at 11:30
  • Not possible synchronously. You can only do parseXMLData(data, function(html){yourVariable = html}); The assignment will be asynchronous. – Ashwin Prabhu Jul 30 '12 at 12:14
  • The right approach is to put everything that follows parseXMLData in another function body which takes one param and pass the function name as second param to papreXMLData in place of window.alert. Here By follows I mean, things that have to be executed after the AJAX call. – Ashwin Prabhu Jul 30 '12 at 12:16
  • 1
    It would pay you rich dividends going forward, if you invest some quality time in learning async programming in JS, callbacks, listeners et al. Hacking around will only frustrate you more. – Ashwin Prabhu Jul 30 '12 at 12:19