How can I figure out the type of a given XML element with jQuery?
For example, if I want the type of the second child:
XML:
<xml>
<a>a element</a>
<b>b element</b>
<c>c element</c>
</xml>
JS:
var node = $(xml).eq(2);
var nodeType = getNodeType(node);
if (nodeType == 'b') {
alert('GOT IT');
}
function getNodeType($node) {
...
}
Try using
nodeNameon the element (not on the jQuery object).Example: http://jsfiddle.net/2cSpq/
I also used the
children()[docs] method to target the nested elements, which would make thebelement at index1, not2.The
[0]extracts the node from the jQuery object,.nodeNamegets the node’s name, and.toLowerCase()ensures it is sent to you as the lower case letter you’re testing for.