0

I have an XML like this:

<?xml version="1.0"?>
<service>
    <name>My Element</name>
    <header>My Header</header>
    <sections>
        <section>
            <!-- Optional -->
            <subheader>Subheader 1</subheader>
            <description>Here goes the content</description>
        </section>
        <!-- Optional -->
        <section>
            <subheader>Subheader 2</subheader>
            <description>Here goes the content</description>
        </section>
    </sections>
</service>

And I am trying to read the values of the "subheader" and "description" element from each section. However I am unable to retrieve the values of these elements. Here is my code:

function getDescription(descriptor_url) {
        var descriptor_object = $.get(descriptor_url, function(descriptor_data, descriptor_status, descriptor_response) {
            $(descriptor_response.responseXML).each(function () {
                //this works fine
                var header = $(this).find('header').text();
            });
            $(descriptor_response.responseXML).find('sections').each(function() {
                // here is where the issue is
                $(this).find('section').each(function() {
                    var subheader = $(this).find('subheader').text;
                    // Included a screenshot of this alert here
                    alert(subheader);
                })
            })
        })
        .fail(function() { 
           response.value = "Something went wrong with the request.\n\nReason: " + descriptor_object.statusText;
        })
    }

I was thinking that $(this) is local to $.each loop but looks like i'm wrong. Can someone please help?

Thanks!

alert message when i try to print the subheader - looks like a jquery function

1 Answer 1

1

Note, at original post , var subheader = $(this).find('subheader').text; appear to be not have () after .text ?

Try

    $(descriptor_response.responseXML).each(function () {
            //this works fine
            var header = $(this).find('header').text();
            var subheaders = $(this).find("sections section subheader").text();
            var descriptions = $(this).find("sections section description").text();
            alert(header + "\n" + subheaders + "\n" + descriptions);
        });

jsfiddle http://jsfiddle.net/guest271314/hv46r/

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that solved it! Such a silly mistake. Had to change .text to .text(). Thanks a lot for pointing that out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.