Reputation: 17522
is there a way to get the list of attributes set to an element?
example:
<div id="myID" title="I am Title" myAttr="I am something else">Hello World!!!</div>
Is there a way to get all the above attributes?
I tried this already but nothing so far:
$('#myID').attr();
I tried this as well:
$('#myID').attr().each(function(a,b){
alert(a);
});
did not help either... so any suggestions would be appreciated.
thanks.
Upvotes: 7
Views: 5409
Reputation: 20556
You can use the DOM attributes
property on the underlying jQuery element to extract a NamedNodeMap
containing all of the element's attributes. This can be quickly parsed into an object which could be passed directly to .attr()
.
var attrs = {};
var attrMap = $('#myID')[0].attributes;
$.each(attrMap, function(i,e) { attrs[e.nodeName] = e.nodeValue; });
attrs
is now:
{id: "myID", title: "I am Title", myattr: "I am something else"}
Here's a jsfiddle that shows how this works: http://jsfiddle.net/joemaller/cDYtr/
Upvotes: 2
Reputation: 53930
Use this plugin: http://plugins.jquery.com/project/getAttributes
Upvotes: 3