/ Published in: JavaScript
This ignores whitespace and comments.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function getPrevious(el) { function isIgnorable(node) { // is a comment or contains only whitespace return (node.nodeType == 8 || /^[\t\n\r ]+$/.test(node.data)); } var prev = el; while (prev = prev.previousSibling) { if (!isIgnorable(prev)) break; } return prev; }