I would like to use Ajax to check if an XML string ("data") contains a certain value under "voteBy", e.g. the name "John Doe". If yes, then I want to show an alert, if not then show another alert. The idea behind is that I want to do something here later with jQuery but only if the search term is NOT found in the XML string.
I tried the below which is the success function of the Ajax call to fetch this XML string. In this case it should alert "found" as the search term appears in the XML string but my approach doesn't return anything.
Can someone tell me what I am doing wrong here? Note: Data for the XML can vary so there may be more or less or no votes for a certain item.
My XML looks as follows (example data, shortened):
<ranks>
<itemDetails>
<itemID>1</itemID>
<title>Test item</title>
<details><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p></details>
<lastUpdate>Added</lastUpdate>
<modTime>Saturday, 23 August 2014</modTime>
<modBy>Someone</modBy>
<comments>
<commentID>1</commentID>
<comment>Some comment</comment>
</comments>
<comments>
<commentID>2</commentID>
<comment>Another comment</comment>
</comments>
<votes>
<voteBy>John Doe</voteBy>
</votes>
<votes>
<voteBy>Jane Doe</voteBy>
</votes>
</itemDetails>
</ranks>
My jQuery (success function only, shortened) - data here is my XML string:
$.ajax({
// ...
success:function(data) {
if(!$(data).find('voteBy').text() == 'John Doe') {
alert('not found');
} else {
alert('found');
}
}
});