Jquery Find an XML element based on the value of one of it's children

Posted by NateD on Stack Overflow See other posts from Stack Overflow or by NateD
Published on 2010-06-17T17:20:40Z Indexed on 2010/06/17 17:23 UTC
Read the original article Hit count: 270

Filed under:
|

I'm working on a simple XML phonebook app to learn JQuery, and I can't figure out how to do something like this: When the user enters the first name of a contact in a textbox I want to find the entire record of that person. The XML looks like this:

<phonebook>
<person>
    <number> 555-5555</number>
    <first_name>Evelyn</first_name>
    <last_name>Remington</last_name>
    <address>Edge of the Abyss</address>
    <image>path/to/image</image>
</person>
<person>
    <number>+34 1 6444 333 2223230</number>
    <first_name>Max</first_name>
    <last_name>Muscle</last_name>
    <address>Mining Belt</address>
    <image>path/to/image</image>
</person>
</phonebook>

and the best I've been able to do with the jQuery is something like this:

var myXML;
function searchXML(){
  $.ajax({
     type:"GET",
     url: "phonebook.xml",
     dataType: "xml",
     success: function(xml){myXML = $("xml").find("#firstNameBox").val())}
  });
 }

What I want it to do is return the entire <person> element so I can iterate through and display all that person's information. Any help would be appreciated.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery