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.