4

I have the following XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<words>
    <word>
        <id>1</id>
        <name>Teacher</name>
    </word>
    <word>
        <id>2</id>
        <name>Pitcher</name>
    </word>
</words>

And the following jQuery code:

$.ajax({
    type: "GET",
    url: "sites.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('word').each(function(){

        ...

});

How can I get word with ID == 1?

This ajax function is inside another function (getWord() function). I want to get word with any ID and assign that value to a local variable of getWord() function.

How can I do that?

2 Answers 2

3

You can always resort to writing a custom filter.

$(xml).find("word").filter(function () {
  return $(this).find("id").text() == "1";
}).each(function () {
  console.log(this);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I have another question: How can I get the word returned in getWord() parent function.
@VansFannel: You can't. Put your next steps into the success callback.
1
$(xml).find('word').each(function(){

       if(parseInt($(this).find('id').text()) == '1')
{

      // your logic here
}
});

Comments

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.