I have spent considerable time over the last few days trying to figure out how to properly pass the correct input to this javascript function to have it search my XML document and return the requested value(s). The XML piece is working and I am able to load content from XML using the following:
function parse(document){
songLyrics = $(document).find('elementRef[id="2"] content').text()
scriptureRef = $(document).find('elementRef[id="2"] bibleRefs').text()
$("#content").text(songLyrics);
$("#scripture").text(scriptureRef);
};
$.ajax({
url: 'lyrics.xml',
dataType: "xml",
success: parse
});
What I can't get to work for the life of me is to pass an additional string to the existing function (lets call it "reference" as seen below)--the ultimate goal is to pass a new XML search to the function in an onclick event like so:
function parse(document,reference){
songLyrics = $(document).find(reference).text()
$("#content").text(songLyrics);
};
$.ajax({
url: 'lyrics.xml',
dataType: "xml",
success: parse
});
...
<div id="content"></div>
<a href="javascript:;" id="play" onclick="javascript:parse(document,'elementRef[id=\"2\"] artist')">Title</a>
The goal is simply to pass that string (which properly searches the XML and returns the correct value when hard-coded in the function, but when passed to the function via onclick I can't get it to work (the text that is loaded on page load is replaced with nothing after clicking and no errors are generated in the debugging window).
For awhile, I was trying to get it working by using id="2" but I read elsewhere that I should use the " notation and when tested in alert() it appears properly as quotes. I already tried various forms of escaping with both semiquotes and quotes to no avail.
Any help is greatly appreciated.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<elements>
<elementRef id="1">
<name>John</name>
<artist>Smith</artist>
<content>Active</content>
<supportImg>test1</supportImg>
<bibleRefs>Mark 2:13</bibleRefs>
<other>Mark 2:11</other>
</elementRef>
<elementRef id="2">
<name>Jane</name>
<artist>Smith</artist>
<content>Active</content>
<supportImg>test2</supportImg>
<bibleRefs>John 3:17 Mark 12:3</bibleRefs>
<other>October, 2011</other>
</elementRef>
</elements>
No comments:
Post a Comment