views:

1283

answers:

4

Am I allowed to add whatever attributes I want to HTML tags such that I can retrieve their value later on using javascript? For example:

<a href="something.html" hastooltip="yes" tipcolour="yellow">...</a>

If that's not going to work, how would you store arbitrary pieces of information like this?

Edit: Since it appears that making up HTML attributes isn't technically valid, I've rephrased the second part of this question into its own question here: http://stackoverflow.com/questions/432174/

+3  A: 

You can. The page will not pass verification, however most browsers will accept it.

It is almost certainly the wrong way of doing it. Try using class="hasTooltip_yes tipcolour_yellow"

Richard Harrison
you don't think that bastardising the class attribute is even more wrong though?
nickf
If you bastardise the class attribute it will still validate so in terms of adhering to standards your still better off.
Kevin Loney
Just because you are adhering to standards, doesn't mean you have the best possible solution.
Kibbee
+1  A: 

Depending on the information you want to store you should look at some of the other attributes available. These don't look like good candidates, but 'rel' has been used for lots of interesting things.

Kevin Davis
according to the spec, the rel attribute can only contain LinkTypes eg: start next prev chapter
nickf
But, the spec doesn't define an authoritative list of link types, and in fact leaves room for expansion
Gareth
+19  A: 

Well, you can always create your own DTD to get new valid attributes for your tags. Browser won't hiccup, you should test your JavaScript if you can access these custom attributes. Or you can use the existing facilities provided by HTML and CSS. You can use multiple classes like

<a href="..." class="class-one class-two has-tooltip">

For the colour selection I strongly discourage you to use hard-coded colour names in your HTML, you have CSS for declaring styles. Use the class attribute as a hook for the CSS declaration, and choose semantic class names, for example

HTML:

<a href="..." class="has-tooltip common-tooltip">
<a href="..." class="has-tooltip long-tooltip">

CSS:

a.has-tooltip {
   colour: red;
}
a.common-tooltip {
   background: #ddd;
}
a.long-tooltip {
   background: #ebf;
}

And in your JavaScript code you can generate elements with classes like "long-tooltip" and "common-tooltip" instead of "yellow-tooltip", so you won't contradict yourself in case of redesigning the page to have green tooltips.

bandi
the example i gave really was just an example: the data i'd be storing could be anything really, not just colours and whatnot.
nickf
okay, I was just answering defensively :-)
bandi
+9  A: 

In HTML5, yes. You just have to prefix them with data-. See the spec.

Of course, this implies you should be using the HTML5 doctype (<!doctype html>), even though browsers don't care.

Ms2ger
HTML5 is still in draft, this is not a safe recommendation
annakata
Umm, this specification is meant to be used before going final. http://wiki.whatwg.org/wiki/FAQ#When_will_we_be_able_to_start_using_these_new_features.3F
nes1983
This won't break anything anymore than arbitarily choosing attributes, and it's valid for a future spec, smiles all round!
Rich Bradshaw