1

How to copy all user defined attributes for a DOM element into an array? I Unsuccessfully tested to get ["type", "onClick",'value','id'] See the following code

Js

 var obj=document.getElementById('input')/*.attributes*/,
     array=[],
     index=0;
     for(array[index++] in obj);
 console.log(array);

html

<input type='button' onClick="Start()" id='input' value="Start"/>
Bellash
  • 7,560
  • 6
  • 53
  • 86
  • http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery Perhaps this? – briosheje May 27 '15 at 13:33

3 Answers3

3

A Better Answer

I now recommend this answer by Tim Kindberg to the question Get all Attributes from a HTML element with Javascript/jQuery that uses getAttributeNames() and getAttribute(name), which MDN says "is a memory-efficient and performant alternative to accessing Element.attributes."


Use Array.from() or Spread Operator ...

Update: Array.from() and Spread operator ... were added to ECMA-262 6th Edition in June 2015, which now has universal modern browser support.

See MDN › Array.from() & Spread syntax (...)

var elem  = document.getElementById('input'),
    attrs = elem.attributes;

console.log('Array.from(attrs)');
Array.from(attrs).forEach(({ name, value }) => {
    console.log(`    ${name}: ${value}`);
})

console.log('[...attrs]');
[...attrs].forEach(({ name, value }) => {
    console.log(`    ${name}: ${value}`);
})
<input type='button' onClick="Start()" id='input' value="Start"/>

Note: The following is the legacy answer. It will still work, but the newer Array.from() method is now preferred. This could now be considered a polyfill to support pre-ES2015 targets.

Use .slice to convert the attributes property to Array

The attributes property of DOM nodes is a NamedNodeMap, which is an Array-like object.

An Array-like object is an object which has a length property and whose property names are enumerated, but otherwise has its own methods and does not inherit from Array.prototype

The slice method can be used to convert Array-like objects to a new Array.

var elem  = document.getElementById('input'),
    attrs = elem.attributes;

console.log('Array.prototype.slice.call(attrs)');
Array.prototype.slice.call(attrs).forEach(
    function (cur) {
        console.log(cur.name + ': ' + cur.value);
    }
)
<input type='button' onClick="Start()" id='input' value="Start"/>
gfullam
  • 11,531
  • 5
  • 50
  • 64
  • 1
    You can also just call `array = Array.from(obj);`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from – WoodrowShigeru Apr 30 '22 at 09:18
  • 1
    That's a great callout. This answer was written in May 2015, but `Array.from()` was not added to [ECMA-262 until the 6th Edition in June 2015](https://262.ecma-international.org/6.0/#sec-array.from). It did not receive wide browser support until later that year — and never was available in IE which was still a concern at the time. I'll update the answer though. It's about time. :) – gfullam May 02 '22 at 16:04
1

The attributes are available on the attributes NamedNodeMap; you access each one with [] notation, and there's a length.

So:

var array = Array.prototype.map.call(
    document.getElementById('input').attributes,
    function(attr) {
        // Use nodeName for the name, nodeValue for the value
        return attr.nodeName;
    });
snippet.log(array.join(", "));
<input type='button' onClick="Start()" id='input' value="Start"/>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="https://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
ptim
  • 14,902
  • 10
  • 83
  • 103
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Get all Attributes from a HTML element with Javascript/jQuery

Using it with your JS, you get:

var obj=document.getElementById('input')/*.attributes*/,
     arr=[],
     index=0;

for (var i = 0, atts = obj.attributes, n = atts.length; i < n; i++){
    arr.push(atts[i].nodeName);
}
 console.log(arr);
Community
  • 1
  • 1
Thalsan
  • 5,140
  • 1
  • 11
  • 12