jQuery Plugin: getAttrs
get all attributes of an element
by SpYk3
HTML
<input id="Install" name="ready_not" onchange="$('#Weeding').removeAttr('checked');setWeedingContent();doPrice();" type="radio" value="Install" />
<li class="weed_yourself">
<input id="Yourself" name="ready_not" onchange="$('#Weeding').attr('checked','checked');setWeedingContent();doPrice();" type="radio" value="Yourself" />
<label class="select-label" for="Yourself">Weed and Tape Yourself</label>
</li>
</ul>
<hr />
JavaScript
;;(function($) {
if (!$.getAttrs) {
$.extend({
getAttrs: function() {
var args = arguments,
strAttr,
elm;
if (args.length) {
for (x in args) {
switch (typeof args[x]) {
case "object":
args[x] instanceof jQuery && (elm = args[x]);
break;
case "string":
elm ? strAttr || (strAttr = args[x]) : elm = $(args[x])
};
}
}
if (elm instanceof jQuery) {
var $return = [];
if (1 == elm.length) {
for (var attr, i = 0, attrs = elm[0].attributes, n = attrs.length; i < n; i++) attr = attrs[i], $return[attr.name] = attr.value;
elm.data("attrList", $return);
strAttr && "all" != strAttr && ($return = elm.attr(strAttr));
}
else {
strAttr && "all" != strAttr ? elm.each(function(a) {
a = {
elm: $(this),
index: $(this).index()
};
a[strAttr] = $(this).attr(strAttr);
$return.push(a)
}) :
elm.each(function(a) {
$elmRet = [];
for (var b = 0, c = this.attributes, d = c.length; b < d; b++) a = c[b], $elmRet[a.name] = a.value;
$return.push({
elm: $(this),
index: $(this).index(),
attrs: $elmRet
});
$(this).data("attrList", $return)
});
}
return $return;
}
else {
return 'Error: Cannot find Selector';
}
}
});
$.fn.extend({
getAttrs: function() {
var args = [$(this)];
if (arguments.length) for (x in arguments) args.push(arguments[x]);
return $.getAttrs.apply($, args);
}
});
}
})(jQuery);
$('#Install').attr('bob', 'bill');
console.log($('#Install'))
console.log(new Array(50).join(' -'));
console.log($('#Install').getAttrs('id'));
console.log(new Array(50).join(' -'));
console.log($.getAttrs('#Install'));
console.log(new Array(50).join(' -'));
console.log($.getAttrs('#Install', 'name'));
console.log(new Array(50).join(' -'));
console.log($.getAttrs('*', 'class'));
console.log(new...