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.

Recent Posts

Unveiling the Mystery: How Long Do Shrooms Last and What to Expect from a Trip

Introduction: Embarking on a psychedelic journey with magic mushrooms, or "shrooms," can be an enlightening…

43 mins ago

5 Techniques to Boost Productivity Through Technology Integration

Early technology adopters often reap the rewards of taking a chance. Let’s take a look…

1 hour ago

5 Tips for a Smoother Move to Dubai

The Emirate of Dubai is a luxury paradise that has many luxury projects including Emaar…

19 hours ago

Career opportunities after completing an ACCA course

The Association of Chartered Certified Accountants (ACCA) is a professional accountancy qualification that is recognized…

21 hours ago

Chasing Legends: Legendary Moments in Online Gaming

In the vast digital landscapes of online gaming, legends are born and moments are etched…

21 hours ago

Virtual Thrills: Adventures in Online Gaming

In the ever-evolving landscape of entertainment, online gaming stands out as a realm where imagination…

22 hours ago