Posted By

nerdfiles on 02/10/10


Tagged

js DOM


Versions (?)

getElementsByAttribute


 / Published in: JavaScript
 

Derivative from Dustin Diaz's getElementsByClass

  1. document.getElementsByAttribute = function( attrib, value, context_node, tag ) {
  2. var nodes = [];
  3. if ( context_node == null )
  4. context_node = this;
  5. if ( tag == null )
  6. tag = '*';
  7. var elems = context_node.getElementsByTagName(tag);
  8.  
  9. for ( var i = 0; i < elems.length; i += 1 ) {
  10. if ( value ) {
  11. if ( elems[i].hasAttribute(attrib) && elems[i].getAttribute(attrib) == value )
  12. nodes.push(elems[i]);
  13. } else {
  14. if ( elems[i].hasAttribute(attrib) )
  15. nodes.push(elems[i]);
  16. }
  17. }
  18. return nodes;
  19. }

Report this snippet  

You need to login to post a comment.