Question

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?

Was it helpful?

Solution

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");

OTHER TIPS

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

If you're storing data to use in javascript, you can also use something like jQuery's Metadata plugin. Basically, you can store data within the element's class="" attribute, like so:

<div id="aaa" class="class1 class2 class3 { type: 'food', color: 'green' }"></div>

Then in javascript:

alert($('#aaa').metadata().color) // "green"

Other kits use the title or rel attributes to store data. While this is more validation friendly, it may or may not be better than using AnthonyWJones' answer of just using non-standard attributes. It'll "break" validation, but then again according to Dojo, custom attributes are perfectly valid HTML, even if they don't validate against a DTD.

So no - there isn't a single well accepted specific attribute where you can dump all data. All existing attributes are for specific uses. But you can either 1) create your own attributes, or 2) coopt an existing tag to reuse for your purposes. Just wanted to point out the alternative.

Have a look at www.htmlref.com or W3C for the used attributes.

Other than those you can just add your own, they will render and they will be accessible via code for instance in C# you can access a controls attribute collection.

Control.Attributes["MyCustomAttribute"] = "Hello World";

there’s rel and rev attributes, which work in elements with an href-attribute. they have a semantic meaning, but are often abused as an attribute to store additional information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top