Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a generic attribute for all HTML elements aside from ID and class?

Like a tag that I can use to store some necessary info? But really isn’t required or used by the HTML? Works like the tag attribute for objects on Visual Basic?

like image 980
lock Avatar asked Sep 16 '09 07:09

lock


2 Answers

Up until HTML5 no. With HTML 5 there is provision for this with the data-* attribute.

For example:-

<div id="myStuff" data-mydata="here is my data">

In current technology there is no "official" away to do this. However all browsers allow you to add any arbitary attribute to a HTML element so in HTML4 you can do this:-

<div id="myStuff" data-mydata="here is my data">

Which as you can see is identical but not offically sactioned and if you want strict XHMTL compliance will be considered "broken".

You can access the attribute just as you would any other:-

var mydata = document.getElementById("myStuff").getAttribute("data-mydata");
like image 109
AnthonyWJones Avatar answered Oct 12 '22 23:10

AnthonyWJones


You could perhaps use the html5 data-* attributes? It'll fail validation on html4, but it is still probably the best option...

like image 24
Marc Gravell Avatar answered Oct 13 '22 01:10

Marc Gravell