views:

42

answers:

2

I'm sending an ajax request that will either give me an error or a userID

<?xml version="1.0" encoding="UTF-8"?>
<error>1000</error>

<?xml version="1.0" encoding="UTF-8"?>
<userID>8</userID>

how can I find out if the root element is error or userID using jquery or just javascript in general. Using the find method doesn't appear to work if what you're trying to access is the root element

A: 

You can call filter or is instead of find.

Unlike find, which searches the descendants of the elements in the jQuery object (but not the elements themselves), these methods search only the actual elements in the jQuery object.

SLaks
+2  A: 

Just use is:

var xml = $('<?xml version="1.0" encoding="UTF-8"?><userID>8</userID>');
if (xml.is('userID')){
  // true
} else {
  // false
}
Doug Neiner