7

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.

3 Answers 3

3

Use this plugin: http://plugins.jquery.com/project/getAttributes

Sign up to request clarification or add additional context in comments.

2 Comments

i dont really like addons/plugins... it would be my last result... i have already seen that, but thanks
Then look at the code of the plugin to see how it's done, eh?
2

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/

Comments

0

Or this one: http://code.google.com/p/jquery-list-attributes/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.