Iterating over element attributes with jQuery

54,898

Solution 1

The best way is to use the node object directly by using its attributes property. The only difference in my solution below compared to others suggesting this method is that i would use .each again instead of a traditional js loop:

$(xml).find('item').each(function() {
  $.each(this.attributes, function(i, attrib){
     var name = attrib.name;
     var value = attrib.value;
     // do your magic :-)
  });
});

Solution 2

it seems you have to use plain old vanilla javascript:

for (var i = 0; i < elem.attributes.length; i++) {
    var attrib = elem.attributes[i];
    if (attrib.specified == true) {
        console.log(attrib.name + " = " + attrib.value);
    }
}

How to iterate through all attributes in an HTML element?

Solution 3

Could you get the DOM element from the jQuery wrapper using the get() function, and then iterate the DOM attributes?

var node = $(myStuff).get(0);
if (node.attributes.length) {

    for (var length = attrs.length, i = 0; i < length; i++) {
        if (attrs[i].specified) {

        }
    }
}

For some much more robust DOM attribute handling, check this blog post.

Solution 4

created this generic function that allows to look-in attributes as well as innearHtml:

function findByAll(toLookFor, lookInText) { 
    return $('*').filter(function() { 
        return Array.prototype.some.call(this.attributes, function(attr) {
            return attr.value.indexOf(toLookFor) >= 0; 
        }) || (lookInText && $(this).text().indexOf(toLookFor) >= 0); 
    }); 
}

Solution 5

How about?

$(xml).find('item').each(function() {
  var attributes = $(this)[0].attributes;
  for (attribute in attributes) {
    // Do something with each attribute...
  }
});
Share:
54,898
theraccoonbear
Author by

theraccoonbear

I'm a developer who's spent my early professional years writing .NET, then quite a bit of time wading through various PHP frameworks with MySQL and PostgreSQL, then a few more years mired in MERN and MEAN, and more recently maintaining legacy Java systems backed by MongoDB while building new microservices with Go using DynamoDB and Redis. I occupy much of my spare time exploring Linux, Perl, some open source projects, and various academic Computer Science and Math type things.

Updated on September 07, 2020

Comments

  • theraccoonbear
    theraccoonbear over 3 years

    I know individual attributes can be retrieved with the attr() method, but I'm trying to iterate over all of the attributes for an element. For context, I'm using jQuery on some XML...

    <items>
      <item id="id123" name="Fizz" value="Buzz" type="xyz">
        <subitem name="foo">
        <subitem name="bar">
      </item>
      <item id="id456" name="Bizz" value="Bazz" type="abc">
        <subitem name="meh">
        <subitem name="hem">
      </item>
    </items>
    

    I am already able to iterate over the items with...

    $(xml).find('item').each(function() {
      // Do something to each item here...
    });
    

    But I'd like to be able to get an array of attributes for each 'item' so I can then iterate over those...

    e.g.

    $(xml).find('item').each(function() {
      var attributes = $(this).attributes(); // returns an array of attributes?
      for (attribute in attributes) {
        // Do something with each attribute...
      }
    });
    

    I've done some searching here, in the jQuery documentation, and elsewhere via Google but have had no luck. If nothing else, I may just be having trouble excluding results relating to the attr() method of the jQuery object. Thanks in advance.