Mayank Pathak
Mayank Pathak

Reputation: 3681

Get values from child XML nodes having same node name in jquery

I've below XML

<Root>
<field/>
<field/>
<field>
    <label>
        Have you invested before
    </label>
    <value>
        No
    </value>
    <label>
        Are you looking to Invest in the next 6 months
    </label>
    <value>
        Maybe
    </value>
</field>
<field>
    <label>
        What Investments are you interested in
    </label>
    <value>
        Carbon Credits, Green Investments
    </value>
</field>
<field>
    <label>
        How much are you looking to invest
    </label>
    <value>
        £250,000+
    </value>
</field>

And from this i need to render it as HTML like as below.

Have you invested before: No
Are you looking to invest in the next 6 months: Maybe
What investments are you interested in: Carbon Credits, Green Investments
How much are you looking to invest: £250,000+

Currently it's output is :

:
:     
Have you invested beforeAre you looking to Invest in the next 6 months: YesMaybe
What Investments are you interested in:  Carbon Credits, Green Investments
How much are you looking to invest:    £250,000+

I'm using below jquery but its not producing the output i'm expecting.

$(data).find('field').each(function (index, element) {
        debugger;
        //if (field.find('label').length > 0) {
        var field = $(element)
        if (field.find('label').length > 1) {

            var confirmationNumbers = $(field).find("label").map(function () {
                //debugger;
                list.append('<dt>' + label + ': </dt>').append('<dd>' + value + '</dd>');
                return $(this).text();
            });                
            //var label = field.find('label').text()
            //var value = field.find('value').text()
            //list.append('<dt>' + label + ': </dt>').append('<dd>' + value + '</dd>');
        }
        else {
            var label = field.find('label').text()
            var value = field.find('value').text()
            list.append('<dt>' + label + ': </dt>').append('<dd>' + value + '</dd>');
        }
        //}            
    });

What i'm doing wrong..

Upvotes: 1

Views: 1601

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$(data).find('field label').each(function(){
    var $this = $(this);
    list.append('<dt>' + $this.text() + ': </dt>').append('<dd>' + $this.text().val() + '</dd>');
})

Try this for IE

$(data).find('field label').each(function() {
    var $this = $(this);

    list.append('<div><span>' + $this.text() + '</span><span>'
            + $this.text().val() + '</span></div>');
})

Upvotes: 2

Related Questions