Rafty
Rafty

Reputation: 703

Get Value from parent node in XML using JQuery

I am trying to get value from the parent after a programme node has been selected, below is the ajax function that loads the XML and finds each of the programmes ID's that are stored in a schedule array.

        $.ajax({
                type: "GET",
                url: "tvguide.xml",
                success: function(xml){

                    //for each schedule ID stored in the array this function fires
                    $.each(schedule_array, function(index, item) {
                        //finds the program ID and then inserts the data from the XML into a UL in the Series Details div
                        $(xml).find("programme[id="+item+"]").each(function(){ 
                            //Get the value from the previous node and store here?
                        });
                    });
                },
                error: function(){
                    alert("XML File could not be found.");
                }
            });

Below is a small example of the XML.

<channel value="channel_1">
        <programme id="1">
            //programmes details in here
        </programme>
 </channel>

Basically I need to get the value of the channel node when the programme within that channel has been selected in the loop above. Is this possible?

Upvotes: 0

Views: 2109

Answers (1)

Milind Anantwar
Milind Anantwar

Reputation: 82231

You can use .parent() to get parent reference and .attr() to get/set attribute value:

$(xml).find("programme[id="+item+"]").each(function(){
   var parentval = $(this).parent().attr('value');
});

Upvotes: 1

Related Questions