8

How can I get the list of attributes of an HTML string using Javascript? Here's my code so far.

function traverse_test(){
    var root=document.getElementById('arbre0').childNodes;
    for(var i=0;i<root.length;i++){
        var lis = root[i];
        if (lis =='[object HTMLUListElement]') {
            for (var member in lis) {
                if (typeof lis[member] == "string") {
                    var assertion = lis[member];
                    var resultat = assertion.search(/..Bookmarks/);
                    if (resultat != -1) {
                        output.innerHTML+= lis[member];
                        // Here I'd like to have the list of lis[member] attributes
                        for(var attr in lis[member].attributes) {
                            output.innerHTML+=lis[member].attributes[attr].name + "=\""+ lis[member].attributes[attr].value + "\"";
                        }
                        break;
                    }
                }
            }
        }
    }
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Bruno
  • 8,497
  • 13
  • 38
  • 55
  • 1
    You mean any arbitrary string of HTML? like `parseHTMLAndGetAttributes("");`? Or do you want to get attributes from the DOM of an already parsed page? The latter is easier. – Joshua Carmody Apr 21 '11 at 14:06
  • From the DOM but I'm going tro try your first method, thanks ! – Bruno Apr 21 '11 at 14:08
  • 2
    @Bruno: that's not actually a method that exists. @Joshua was just using that as a hypothetical use case. – Matt Ball Apr 21 '11 at 14:09
  • Actually the string belongs to an element of the DOM but I have not direct access to it. – Bruno Apr 21 '11 at 14:16
  • @Bruno - yeah, the function name was one I made up just for illustrative purposes. Sorry that wasn't more clear. – Joshua Carmody Apr 21 '11 at 14:18
  • @Bruno - @Matt Ball's answer below looks like it does exactly what you want. Just put `for(var attr in lis[member].attributes) { alert(lis[member].attributes[attr].name + "=\"" + lis[member].attributes[attr].value + "\""); }` in the spot you've indicated in your comment, and you should see the results you want. – Joshua Carmody Apr 21 '11 at 14:44
  • @Joshua Carmody Thanks for your answer but it doesn't work. – Bruno Apr 22 '11 at 07:26

4 Answers4

14

Use the Node.attributes property of a DOM element. Example:

var foo = document.getElementById('foo'),
    attrs = foo.attributes,
    i = attrs.length,
    attr;

while (i--)
{
    attr = attrs[i];
    console.log(attr.name + '="' + attr.value + '"');
}

Demo: http://jsfiddle.net/mattball/j8AVq/

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
7

Seems like all these answers point to how to get an attr list from a node but the question asks for attrs from an HTML string. Here is my 2cents.

//turn your string into a node and get your html strings NamedNodeMap
var temp = document.createElement("div");
    temp.innerHTML  = "<div attr-1 attr-2 attr-3 attr-4></div>";
    temp = temp.firstElementChild.attributes; 

    //put the attributes in a an array
    var list = Object.keys(temp).map( function( index ) { return temp[ index ] } );

    console.log( list );
Lorenzo Gangi
  • 601
  • 7
  • 8
1

If you know the attributes to get the value you can do:

var MyValue = document.getElementById("myimage").getAttribute("src")

In JavaScript to loop all attributes:

var el = document.getElementById("someId");
var arr = [];
for (var i=0, attrs=el.attributes, l=attrs.length; i<l; i++){
    arr.push(attrs.item(i).nodeName);
}

The above code was taken from this question

Jquery might be another option:

http://plugins.jquery.com/project/getAttributes

Community
  • 1
  • 1
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
0
[].slice
.apply(document.querySelector('something').attributes)
.forEach(function(item){
    console.log(item, item.name, item.value);
});