0

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?

1 Answer 1

1

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');
});
Sign up to request clarification or add additional context in comments.

2 Comments

AHHHHHHHH! Thank you so much, I literally have been scratching my head at this for the past couple of hours. Its my first time using AJAX and XML so sorry if the question was silly, but thanks very much!
glad it helps alan :)

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.