DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Using Map On An Array Of Elements

10.09.2005
| 5399 views |
  • submit to reddit
        I wanted to use <a href="http://project.ioni.st/post/245#post-245">prototype's map function</a> on a listing of elements like:

document.getElementsByTagName('div').map(function(el) {});

Object.extend(String.prototype, {
  tags_in: function(el) {
    nodes = [];
    elems = $(el).getElementsByTagName(this);
    for(var i = 0; i < elems.length; i++)
      nodes.push(elems[i]);
    return nodes;
  }
});

'div'.tags_in(document).map(function(el) {});
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:15pm

ah... very handy!

Snippets Manager replied on Mon, 2012/05/07 - 2:15pm

You can use $A instead -- it turns any iterable object (anything that has a .length property and is indexed with the [] operator) into an array. $A(document.getElementsByTagName('div')).map(function(element) {...});