1

How to know the possible HTML attributes of an element (EG ) in JS?

How can I get to know a list of possible HTML attributes for a particular HTML node?

For example, when writing a userscript, I would like to know the possible HTML attributes of such nodes:

var images = d.getElementsByTagName('img');

something like: images[i].src

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Michał Tajchert
  • 10,333
  • 4
  • 30
  • 47

3 Answers3

1

Browsers do not expose that information. You would need to get or create a machine readable specification for the markup language you are using.

HTML 4, for example, has DTDs that you could parse:

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Iterate over the properties of an object with for in like this:

var keys = [];
for (var key in obj) { 
  if (obj.hasOwnProperty(key) {
    keys.push(key);
  }
}

Or use Object.keys(obj) which will return an array of keys, but this will not work in older browsers – see compatibility table.

UPD. Misunderstood the question, for all possible values for DOM element you need to look at HTML DTDs as in Quentin's answer. It would still be possible to set arbitrary attributes though.

Community
  • 1
  • 1
Misha Reyzlin
  • 13,736
  • 4
  • 54
  • 63
-2

Try this Demo Here

var images = document.getElementsByTagName('img');
var allAttributes =Object.keys(image[0]);
Sudarshan Tanwar
  • 3,537
  • 3
  • 24
  • 39