Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <ul>
    <li><a href="#">A</a><span></span></li>
    <li><a href="#">B</a><span></span></li>
    <li><a href="#">C</a><span></span></li>
  </ul>
</body>
</html>
 
/*jshint expr:true eqnull:true boss:true */
var compose = function() {
  return [].reduce.call(arguments, function(f,g) {
    return function() {
      return f(g.apply(this, arguments));
    };
  });
};
var join = function(f,g) {
  return function() {
    var as = arguments;
    return f.apply(this, as).concat(g.apply(this, as));
  };
};
var dot = function(prop) {
  return function(x) {
    return x[prop];
  };
};
var loop = function(prop) {
  return function(x) {
    var result = [];
    while (x = x[prop]) {
      result.push(x);
    }
    return result;
  };
};
var $ = (function(doc) {
  
  var toArray = function(x) {
    if (!x.length || typeof x != 'object') {
      return x; 
    }
    return Array.prototype.slice.call(x);
  };
  
  var flatten = function(xs) {
    return Array.prototype.concat.apply([], xs);
  };
  
  var unique = function(xs) {
    return xs.filter(function(x, idx) {
      return xs.indexOf(x) == idx;
    });
  };
  
  var query = function(sel, el) {
    return toArray((el||doc).querySelectorAll(sel));
  };
  function Dom(sel) {
    this.el = query(sel);
    this.first = this.el;
    this.length = this.el.length;
  }
  
  function $(sel) {
    return new Dom(sel); 
  }
  
  Dom.prototype = {
    _update: function(result) {
      this.prev = this.el;
      this.el = [].concat(result);
      this.length = this.el.length;
      return this;
    },
    get: function(idx) {
      return idx == null ? this.el : this.el[idx];
    },
    map: function(f) {
      var result = this.el.map(compose(toArray, f).bind(this));
      return this._update(unique(flatten(result)));
    },
    filter: function(sel) {
      var result = this.el.filter(function(x) {
        return query(sel, x.parentNode).indexOf(x) > -1;
      });
      return this._update(flatten(result));
    },
    end: function() {
      return this._update(this.first);
    },
    back: function() {
      return this._update(this.prev);
    },
    eq: function(idx) {
      return this._update(this.el[idx]);
    }
  };
  
  return $;
  
}(document));
// Example:
// Use these functions with `map` to chain
// Or you can also add them as methods
var children = dot('children');
var parents = loop('parentNode');
var prevAll = loop('previousElementSibling');
var nextAll = loop('nextElementSibling');
var siblings = join(prevAll, nextAll);
var text = dot('textContent');
var els = $('li').map(parents).get();
console.log(els);
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
elclanrspro
0viewers