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:
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.
People usually stumble onto the 461 visa when they realise there’s no simple way to…
In today’s digital era, people look for convenience, transparency, and genuine rewards from their financial…
If you are planning to start a lighting business and looking for a supplier of…
If you are a copywriter or a content marketer, you will second my statement that…
Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…
No business owner starts his or her business with the hope of making losses. Everyone…