How do I select XML elements with attribute "name" of value "lastName" using JQuery? -


i'm trying create family tree using xml , javascript (/jquery). ask user input name, , search xml file. split name variable firstname , lastname, , search using jquery selector.

as can see below, after i've directly set lastname equal string "lawlor", not able select xml element via "family[name=lastname] selector (as evidenced missing console log, i've included way @ bottom).

a portion of xml file below, followed code i'm using select elements have family element in "name" attribute equal variable "lastname".

what frack doing wrong?

<xml id="familytree" class="hidden"> <generation0>     <family name="lion" surname= "dt" children="4">         <father>joe</father>         <mother>schmoe rose</mother>         <child>lu</child>         <child>bob</child>         <child>sam</child>         <child>dick</child>     </family>     <family name="lawlor" surname="jr" children="5">         <father>jk</father>         <mother>tulip</mother>         <child>holden</child>         <child>ewell</child>         <child>boo</child>         <child>scout</child>         <child>john</child>     </family>            ...  $("input#submitbutton").click( function () {     name  = $("input#txtfield")[0].value;     var firstname = name.split(" ")[0];     var lastname = "lawlor";     console.log("your last name '"  + lastname + ",' , first name '" + firstname + ".'");      $.ajax({         url: 'familytree.xml',         type: "get",         datatype: "xml",         success: function(xml) {             $(xml).find("family[name=lastname] child:contains(firstname)").each(function() {                 console.log("what frack?!");                                 })         },         error: function(xmlhttprequest, textstatus, errorthrow) {             alert('data not loaded - ' + textstatus);         }     }); }) 

jqmigrate: logging active jquery....1.0.js (line 20) last name 'lawlor,' , first name 'john.' familytree.js (line 19)

you need use lastname , firstname variables holds value searched

        $(xml).find("family[name=" + lastname + "] child:contains(" + firstname + ")").each(function() {             console.log("what frack?!");                             }) 

demo: plunker


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -