jquery loop on an elements attributes

1. How do I loop and console.log (or alert) an elements attributes names and values.
2. How can I do the same thing, but by passing in a "starts with" string.  For example
$(element).getAttributes()
$(element).getAttributesStartsWith("customprefix_")

Thanks!
jackjohnson44Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BuggyCoderCommented:
$('#itemId').each(function() {
  $.each(this.attributes, function(i, attribute){
     var name = attribute.name;
     var value = attribute.value;
  });
});

Open in new window

designatedinitializerCommented:
<html>
<head>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script>
function attrLogToConsole(itemID){
	$(itemID).each(function() {
	  $.each(this.attributes, function(i, attribute){
		 var name = attribute.name;
		 var value = attribute.value;
		 console.log("Name:"+name+"  \tValue:"+value);
	  });
	});
}
function attrStartsWith(itemID, starts){
	$(itemID).each(function() {
	  $.each(this.attributes, function(i, attribute){
		 var name = attribute.name;
		 var value = attribute.value;
		 if(name.indexOf(starts) == 0) console.log("Name:"+name+"  \tValue:"+value);
	  });
	});
}
$(document).ready(function() {
	console.log("Attributes for #field:");
	attrLogToConsole('#field');
	console.log("\nAnd now, for something completely different:");
	attrStartsWith('#field', 'si');
});
</script>
</head>
<body>
<form name="form1" action="" method="post">
<input type="text" id="field" size="40" name="subject" Value="This is an input field" />
<input type="submit">
</form>
</body>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjperezaguinagaCommented:
Greetings jackjohnson44,

First, you are looking for a functionality that it's not implemented within jQuery but inside the Javascript definition. JQuery doesn't have a getAttributes method, although can be implemented easily (See code at the end of the answer). From there, you just want to check if the first attribute is the one you want.

I strongly advice experts to use http://jsfiddle.net/ when answering this kind of questions, so we don't clutter the responses with codes. There are multiple tutorials about it, in case you need one.

Alas, the code: http://jsfiddle.net/HZKRT/

Hope it helps,
-JJ
designatedinitializerCommented:
JJ:

I strongly advise you not to advise me.
I stand by my code. I don't stand by 3rd party code.
(except jQuery, in this case...)

jsfiddle is nice, thanks, but... no, thanks.
You just need to copy and paste my code to see it simply works.
You would have saved yourself the trouble.
jjperezaguinagaCommented:
designatedinitializer,

Don't take it personal :) By relying in a third party you can easily get more feedback from people outside EE, update your examples without having to add an extra comment (which often leads to making the authors read all the comments to check the final version), fork other's people examples (like other's experts contributions), separate the three layers of the web (behavior -js-, content -html- and presentation -css) in a easy, sintax highlighted way. These are just a few pros I find in using jsfiddle; I would to hear some cons.

I don't doubt your code works, but I feel that a click in a link is faster than copying and pasting.

Finally, by all means feel free to advice me: I wouldn't be half the developer I am without the advice and help of my peers, friends, experts and co-workers.

Cheers!
-JJ
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.