We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

micmath on 08/23/07


Tagged


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

inamorix
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.