Wednesday, September 25, 2013

Javascript: Getting the next element node with cross-browser compatibility

Getting the next element node in IE is a pain. IE does not support the nextElementSibling method of the DOM Element object and using the nextSibling method also returns text nodes.

So I wrote the javascript function below which will only return an element node (node type of 1).

function getNextElement(srcElement) {
      var nextElement = srcElement.nextSibling;
      while (nextElement.nodeType != 1) {
            nextElement = nextElement.nextSibling;
      }
      return nextElement;
}

No comments:

Post a Comment