X interview question

Implement the getElementsByClassName(element, className) function in Javascript.

Interview Answer

Anonymous

Jan 23, 2012

Walk the DOM from the given parent node, looping along the element.nextSibling pointer. If the current node in the loop has children, you can call the getElementsByClassName function recursively. Keep in mind that it's not a straight string comparison, because elements can have multiple classes.

2