Posted By

micmath on 08/23/07


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

n00ge


Get previous node.


 / Published in: JavaScript
 

This ignores whitespace and comments.

  1. function getPrevious(el) {
  2. function isIgnorable(node) {
  3. // is a comment or contains only whitespace
  4. return (node.nodeType == 8 || /^[\t\n\r ]+$/.test(node.data));
  5. }
  6.  
  7. var prev = el;
  8. while (prev = prev.previousSibling) {
  9. if (!isIgnorable(prev)) break;
  10. }
  11.  
  12. return prev;
  13. }

Report this snippet  

You need to login to post a comment.