views:

450

answers:

5

A very general question.

I am dynamically generating a form that is split into multiple levels of tabs using HTML / JavaScript.

I want to highlight some of the fields (those that have a value differing from a global template) with a star * symbol using CSS and background-image.

A JS field iterates through each field, compares its value, and sets a CSS class for it if necessary. So far, so good.

Now in addition, I want not only every changed field to be marked with a star, but also the tab it is in.

Because I love it simple, I would like to store the element ID of the tab as an attribute somewhere in each field's HTML tag (something like "parentTab"). The JS function then highlights the field and its "parentTab" element (and maybe that one's "parentTab" as well).

My first approach is to misuse the "title" attribute or somethiong to store parentTab in. Of course, that's dirty. However, if I just wildly add arbitrary attributes to the DIV or INPUT tag, it won't validate any more and I feel less than secure accessing these attributes - who knows how different browsers handle it, and will handle it in the future?

So my question is: Is there a valid, standards compliant way - an attribute of some sort - to store arbitrary data inside HTML tags, for further processing by JavaScript?

Of course, I could create a "parentTabs" JS array and be done with it. But storing it in the input itself would be so much more elegant.

+10  A: 

with the introduction of html5, you can use attributes that start with data-, and they'll still validate.

<input type="text" name="username" data-parentTab="tab1" value="non-default">
peirix
Well thats great for the 5-10% of the population that has HTML5 capable browsers.
cletus
Spot on. @OP: **And** you can start using it right away, even though the spec isn't finished. Just change your doctype declaration to `<DOCTYPE HTML>` (that's the whole thing; HTML5 makes doctypes a *lot* easier to remember).
T.J. Crowder
@cletus: It won't matter for older browsers, as it isn't really doing anything except hold information. This will work perfectly fine in IE6. It just won't validate as XHTML or HTML4...
peirix
@cletus: Not so, non-HTML5 browsers universally ignore attributes they don't understand, so you can use this technique right now today, safe in the knowledge that it's future-proofed. Here's some good reading on it: http://ejohn.org/blog/html-5-data-attributes/
T.J. Crowder
Thanks for this - this will come in handy in the future.
Pekka
+2  A: 

You could find out which tabbed element a field belongs to by writing an isChildOf function, like this: http://jimkeller.blogspot.com/2008/07/jquery-ischildof-is-element-child-of.html

Using the DOM to work this out will always be more "elegant" than duplicating the data in some custom format.

Ben James
that requires jQuery, though... But this should also be fairly easy without jQuery.
peirix
"Using the DOM to work this out will always be more "elegant" than duplicating the data in some custom format." - point taken. I will for the moment probably go for the "rel" attribute as it is so quick to do; on the long run, the DOM approach is right.
Pekka
+1  A: 

In jQuery you have the Data API for the matter. http://docs.jquery.com/Internals/jQuery.data

If you do not use jQuery, you can add your own tags and store anything in it. everybody will tell you this is not nice etc, but all the great web firms are doing so. so you will end up in a great company ;-)

Tzury Bar Yochay
+2  A: 

The rel attribute is often a great valid choice for storing data like this.

Alex Sexton
True, and I will go with it for the moment. Still, it is blatant misuse of the attribute, I was looking for ways around that :)
Pekka
A: 

I like how John Resig did this: a script tag with arbitrary type. His example is about templating, but you could really use this for anything.

Jaanus