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

  • submit to reddit
        I wanted to use <a href="">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++)
    return nodes;

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


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

ah... very handy!

Snippets Manager replied on Mon, 2012/05/07 - 1: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) {...});