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 09/12/07


Tagged


Versions (?)


Find any node


Published in: JavaScript 


Lets you find every node in a document based on any test.

  1. function findElements(test, container) {
  2. if (!container) container = document;
  3. if (!container.all) container.all = container.getElementsByTagName('*');
  4. var result = [];
  5. for(var i = 0; i < container.all.length; i++) {
  6. var el = container.all[i];
  7. if (test(el)) result.push(el);
  8. }
  9. return result;
  10. }
  11.  
  12. // For example, get every node with a certain class name like...
  13. // <div class="movable"> and <p class="hilighted movable">
  14. var movables = findElements(function(el){return el.className.indexOf('movable') > -1});

Report this snippet 

You need to login to post a comment.