0

I need to read an attribute or node from my xml file. It is on the same server as the html page. Is it wrong to use an ajax call or should I use more native js to extract the data? I need to access the data in Jquery to dynamically output it in html. I don't wont to loop through the whole xml file really, all I want to do is get one of the child's of Picture. I know in php I can write: $questions = (string)$xml->question[2];
as an example and that would get me the third question in my xml file. I want to know the equivalent to that line to get the second element in jquery. In my code I have

 <script language="JavaScript" type="text/javascript">

    $(document).ready(function(){
        $.ajax({
            type: "GET",
            url: "Pictures.xml",
            datatype: "xml",
            error: function(jqXHR, textStatus, errorThrown) {
                console.log('Error: ' + errorThrown);
            },
            success: function(xml) {
                console.log('AJAX Request is succeded.');




 title = 

  $(xml).find('Picture')(1).find('title').text();//this line wont wrong

       document.getElementById("picture").innerHTML = title;         


            }
        });
    });


</script>

So can anyone help me find just the title for the second Picture title in the xml file.

1 Answer 1

1

You have to use .eq(index) to get the element with particular index from a collection. Please read here to know more about it.

Try this,

$(xml).find('Picture').eq(1).find('title').text();
Sign up to request clarification or add additional context in comments.

1 Comment

@james It will simply get the second element from that element collection as you have asked for. I just added a link in my answer. Just read it for your reference.

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.