Thursday, June 26, 2014

How To Get All Attributes of an Element Using jQuery ?

You need to use attributes property that contains them all:

$(this).each(function() {
  $.each(this.attributes, function() {
    // this.attributes is not a plain object, but an array
    // of attribute nodes, which contain both the name and value
    if(this.specified) {
      console.log(, this.value);
If you want to grab only the attributes that has prefix like data-* from image tags. Use data() property. data-* attributes are supported through .data() and will return all of the data- attribute inside an object as key-value pairs.
$('img').each(function() {
   var data = $(this).data();
    $.each(data, function(key, value) {    
      console.log(key, value);


No comments:

Post a Comment