After some coding, here is a solution I've finally came up with:
$.fn.linqSelect = function(n, fn) { var vs = [], f, t, i, v, r, a = $.isArray(n); $(this).each(function() { if (f = (t = $(this))[fn]) { if (a && (r = { })) for (i in n) r[v = n[i]] = f.apply(t, [v]); else r = n!=null ? f.apply(t, [n]) : f.apply(t); vs.push(r); } }); return vs; };
Possible usages:
$("a").linqSelect('href','attr');Will return an array of all A's href attributes
$("input:radio[data-name][data-value]").linqSelect(['name','value'] ,'data');Will return an array of objects {name:...,value:...} filled with corresponding data
Also one shortcut function :
$.fn.linqSelectData = function(n) { return this.linqSelect(n, 'data'); };Usage:
$("input:radio[data-name][data-value]").linqSelectData(["id", "value", "sid"]);That idea mainly shows the direction of how to implement other linq-like functions
No comments:
Post a Comment