So today I needed a way to get all of an Elements Attributes.
I was limited to get() and getProperties() both require the name of the attribute or property you are after such as src or text or rel… you get the idea.
So simply created my own implementation on the Element Class call allProperties.
This will allow you to take any element and return all attributes as well as text and html if present as an object.
The Mootools Code:
Element.implement({
allProperties: function(){
for (var attr, j = this.get(["text","html"]), i = 0, len = this.attributes.length; i < len; ++i) {
attr = this.attributes[i];
j[attr['nodeName']] = attr['nodeValue'];
};
return j;
}
});
Syntax:
var myProps = myElement.allProperties();
Arguments:
none
Example:
HTML
<div id="me" rel="you" class="test"><span>what?</span></div>
JS
$('me').allProperties()
Returns
{
class: "test",
html: "<span>what?</span>",
id: "me",
rel: "you",
text: "what?"
}
See it in action http://jsfiddle.net/KFS6t/3/
Update: 04/04/2012 http://jsfiddle.net/KFS6t/6/
Optimized for speed by: Dimitar Christoff – http://fragged.org/