Parsing XML With jQuery

Posted on 2011-10-25
Last Modified: 2012-05-12
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 () {
            type: "GET",
            url: "xml.aspx",
            dataType: "xml",
            success: parseXML
        function parseXML(xml) {
            var strOutput;
            $(xml).find('root3').each(function () {
                strOutput = strOutput + $(this).text() + ' - ';

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
Question by:MinnRick
    LVL 81

    Accepted Solution

    first loop :
    from line 11, strOutput is undefined, to prevent replace line 11 by : var strOutput = "";

    Author Closing Comment

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

    Featured Post

    Hire Freelancers to Complete JavaScript Projects

    Source the talented Expert Exchange community
    for top quality work on your JavaScript projects.

    Hire the best. Collaborate easily. Get quality work.

    Join & Write a Comment

    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now