Link to home
Start Free TrialLog in
Avatar of MinnRick
MinnRickFlag for United States of America

asked on

Parsing XML With jQuery

Hello Experts -
I have the following block of jQuery in a web form:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: "xml.aspx",
            dataType: "xml",
            success: parseXML
        });
        function parseXML(xml) {
            var strOutput;
            $(xml).find('root3').each(function () {
                strOutput = strOutput + $(this).text() + ' - ';
                alert(strOutput);
            });
        }
    });
</script>

Open in new window

For purposes of this question assume that the target XML is properly formed and contains three elements called <root3>, each containing the text 'fubar'.  I would expect the alert within the parseXML function to fire three times, the first displaying 'fubar -', the second displaying 'fubar - fubar -' and the third displaying 'fubar - fubar - fubar -'.  The first firing instead displays 'undefinedFubar -'.  Can someone please tell me why, and what I have to do to get rid of that initial undefined reference?  Thanks much.

-- Rick
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MinnRick

ASKER

Yep, thanks.  That one was a  *duh*  on my part - the variable IS undefined until you give it a value.  Much appreciated.