Hi there. Was wondering if anyone could help me on this (hopefully straightforward) question:
I have a test function that accepts an element parameter, and attempts to use DOM to find all child anchor elements below this node, and then find the class attribute for each anchor. The code should look like:
The list returned from getElementsByTagName contains objects that satisfy the Node DOM contract. This Node object does not contain a getAttribute method, whereas Element does. To get this to work with IE, I had to use:
But I'm not sure how cross-browser friendly that solution is? Its been a while since I used Javascript, so I'm probably missing something obvious?
Thanks, Neil
I have a test function that accepts an element parameter, and attempts to use DOM to find all child anchor elements below this node, and then find the class attribute for each anchor. The code should look like:
Code:
function showChildrendAnchorClasses(var inputElement) {
var nodeList = inputElement.element.getElementsByTagName("a");
for (var i = 0; i < nodeList.length; i++) {
var node = node.item(i);
// what must I do to coerce/cast the Node to an Element?
var elem = ??
alert(elem.getAttribute("class"));
}
}
Code:
function showChildrendAnchorClasses(var inputElement) {
var nodeList = inputElement.element.getElementsByTagName("a");
for (var i = 0; i < nodeList.length; i++) {
var node = node.item(i);
alert(node.className);
}
}
Thanks, Neil