DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
jQuery

Getting all element attributes values in jQuery

Share

Here is little example of getting element attributes values with the help of jQuery and store them in an object.

The element’s attributes property is incredibly handy when we want to see what attributes are present in the element without explicitly knowing which to look for. The attributes property provides an easy level of dynamism.

Most of these attributes are available as DOM node properties through JavaScript.

Some of the more common properties are:

  • className
  • tagName
  • id
  • href
  • title
  • rel
  • src

jQuery provides an easy way to manipulate element attributes and access to the element so that we can change its properties also.

For example, we have following XML node:

 <item id="item_01" name="item name" image="image.jpg" />

So, if we use the following loop through all then we can get element attributes.

 var element = $("#item_01");
 var attributes = {};
 $.each(element.get(0).attributes, function(i, attrib){
 attributes[attrib.name] = attrib.value;
 });

So, after running the above snippet on the node we can get access the values on the attributes object we created like so:

 console.log( attributes.id ) // item_01
 console.log( attributes.name ) // item name
 console.log( attributes.image ) // image.jpg

Read Also: 5 Free jQuery Scroll to top Plugins

The attributes property contains them all.

 $('#item_01').each(function() {
 $.each(this.attributes, function() {
 if(this.specified) {
 console.log(this.name, this.value);
 }
 });
 });

In this way you can get all attributes of an element in jQuery.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Edge Computing vs Cloud Computing: What’s the Difference?

Let’s face it. Tech buzzwords get thrown around a lot—especially when it comes to how…

3 days ago

How Data Intelligence Shapes the Future of Digital Innovation

In today’s digital world, the boundaries between technology, finance, and innovation are rapidly disappearing. Businesses…

6 days ago

How To Power Your Backyard Parties with A Solar Power Battery?

Backyard gatherings like BBQs, family reunions, and garden parties are an exciting way to enjoy…

6 days ago

5 Best Practices for Programmatic Advertising

Marketers are always on the lookout for more effective ways to reach their target audiences.…

7 days ago

Are We All Addicted to Stimulation? The New Face of Anxiety Disorders

Does your phone control your mind more than you control your phone? Modern life exploits…

1 week ago

What Lifestyle Changes Help Prevent BPH from Coming Back

Did you know that the prostate continues growing throughout a man's entire life, making BPH…

1 week ago