tags:

views:

1472

answers:

13

I have a site I'm working on where I want to mark if a row of data has been changed. If something has been changed in that row I would mark it with a custom attribute like so.

<tr>
  <td isDirty="true">
     ....row data
  <td>
</tr>

This works great with jQuery and it doesn't add to much code to my page.

But is this really the correct way of doing something like this and what are the downsides?

I guess another way of doing this would be like this, but it seems like over kill.

<tr>
  <td>
     ....row data
     <input id="isDirty" type="hidden" value="true" />
  <td>
</tr>
A: 

I strongly recommend to stick to standards. Web is already messed up ;)

aivarsak
+2  A: 

This will prevent your page from validating I think, since you are adding custom attributes that the validators won't know about.

You might be able to do this with your own DTD or schema, but that's more work.

Your second example is the right way to do this I think. The right direction anyway.

Genericrich
+18  A: 

Technically you should be using the class attribute for this. Tags can have more than one class so it shouldn't affect anything

<td class="isDirty otherclass">
Jamie Lewis
this is true (+1) but comes with *serious* health warnings - making something class="somequality 10" is completely unacceptable and way too common. Best practice: class is an OO construct so think in OO terms when you use it.
annakata
This isn't the answer to the general query but a response to the specific example.
AnthonyWJones
sure, but there's a responsibility to the general case on SO for the benefit of later readers
annakata
+1 to using class. I use class to store 'status' information sometimes because it affords me a CSS styling hook aswell as a reference point for the javascript. HTML5 does offer the 'data' attribute, worth a look but it'll still invalidate non HTML5 pages.
sanchothefat
I like using the class, but I feel like the class attribute is like a dumping ground for everything. What if down the road I end up creating a css style class thats named isDirty? Whats the downside if I just add the custom attribute?
Donny V.
I would not abuse class for this. Class already has its meaning, and putting random values there may confuse styles. I think custom attribute is better choice (I would prefix it so that it's clear that it is really custom attribute, and not some obscure HTML attribute)
Peter Štibraný
@annakata: huh?
AnthonyWJones
@Anthony: It might be useful to answer the general question as well, in case someone else with a similar (but not exactly the same) concern finds this question hoping for guidance.
Adam Bellaire
+1, concise and informative.
Peter Perháč
+5  A: 

HTML 5 supports custom tags prefixed with "data-". So, I would use that to be forward-compatible.

As for earlier versions, I don't think it will validate, but I wouldn't worry about that. I've heard that some browsers may ignore these tags, though. So, be sure to test it all around.

EndangeredMassa
HTML 5 is still in draft phase and support is limited at best, so there's no sense putting the cart before the horse. Also, as Jamie pointed out, the correct solution is to use CSS classes, which would apply even if you were allowed to make your own attributes - that's what they're there for.
Daniel Schaffer
No, CSS classes are there for applying styling, not storing data. They just happen to support data storage because they don't complain if no class is defined with the name you give it. It's still probably a better solution, because of browser "support", to just use a class.
EndangeredMassa
@Massa: I don't agree. Classes are there to say what kind of instance an object is. <div class="employee"> is more correct than <div class="blueText">, and hence you could argue that proper class assignment is more about adding metadata to the object than styling it!
Ola Tuvesson
I suppose you're right. If consider HTML by itself, then the class attribute is just describing the type of the element. In that case, then adding types that don't have css styles would make sense. However, if you want to add a value to an element, I don't think that classes are correct.
EndangeredMassa
+2  A: 

Sticking to the standards for the sake of sticking to the standards is a bad reason to stick to standards.

Your page specifies the DTD, your server specifies the mimetype. As long as you aren't sending real xhtml there is no reason not use expando attributes in this way. It can be very helpful.

Ok so your page won't 'validate' if you need to care about that then don't do it.

Just don't be seduced by IEs 'conveniant' elem.isDirty method of accessing such values. Always use elem.getAttribute('isDirty') to access the value.

AnthonyWJones
Yeah, but what happens to the Next Guy that has to maintain this code? If he uses a class, it is very obvious what it is doing, and some good assumptions can be made about how it is handled by the client script. That can't really be said of adding whatever attributes you like into the code.
Daniel Schaffer
I think custom attributes tell you more about special handling code, while using class may simply mean that someone wanted to apply style for this element.
Peter Štibraný
@Daniel: such techniques aren't just done with a 'Maverick what the heck' attitude. When in a team one establish 1) that this is ok and 2) a consitent approach to doing it. For example, I use it on TRs for rowIDs. It becomes an understood approach with-in the team
AnthonyWJones
@Anthony: Surely the whole reason to have standards in the first place is so that everyone can agree what to stick to!? Sticking to standards for the sake of sticking to standards is a really good reason to do so!
Ola Tuvesson
+4  A: 

Why don't you use the jQuery data capability?

Your example will be (I don't know the condition to select proper td):

$("tr td").data("isDirty", true);

take a look at documentation

tanathos
That can get ugly in server side generation which is the only point it makes sense to add such an attribute to the html.
AnthonyWJones
This would be good, but the data extension does not support an array of data with the same selector.
Donny V.
+1 for the idea that led to my answer.
Donny V.
A: 

I believe that there is not a "Yes" or "No" answer to your question. Using custom attributes will help you have cleaner and shorter code in many circumstances. Your example is trivial and I believe that it can't demonstrate the power and flexibility of this technique. Specifically you are dealing with a boolean attribute, which most likely has to do with appearance. These kind of things are better handled with a class. However, with custom attributes you could add context to your nodes. You could use this context to do some calculations. For example, add a price or a weight attribute to the nodes of the list. You then later use these fields to calculate the sum or average. I am sure that there are many better than this examples out there.

On the other hand custom attributes will prevent the html from passing validation testing. This is an important problem and it may get even more important in the future.

kgiannakakis
A: 

Using custom attribute is useful from several points of view, but be sure to avoid namespace-collision by using a prefix:

<td custom:isDirty="true">

Otherwise it might clash with future implementations.

As a sidenote, a DOM-element can have custom properties like any other js-object, meaning that if you create nodes dynamically you can still add custom data of any type to your element, without affecting the validation.

var element = document.createElement('div');
element.myCustomObject = { foo: "bar", someFunction: function () { alert('foobar'); }};

document.body.appendChild(element);

Of course, the above doesn't make sense to people that never done clientside scripting without frameworks like jQuery, prototype etc... :)

jishi
A: 

isDirty is a perfect example of how a class should be named. Class names should be semantic, and I can't think of a better way of assigning a class to describe what it is about that element (other than to use title if you want to share that information with the end user)?

You shouldn't set your own attributes unless you're willing to maintain your own DTD.

Steve Perks
I see that this response got down-voted twice and up-voted twice. Are there no explanations about why this response does or doesn't jive? I'm willing to take peoples input.
Steve Perks
I didn't vote on your answer, but I would guess it is your last sentence. If I add some attributes and it works fine everywhere, I don't need a DTD. The standards are ruined, at this point just do whatever the hell works.
NateS
A: 

I think with all the different answers here I'v decided not to use expando attributes or classes.

I'll just save the IsDirty state in an array with the rowid on the client side. Then I can later use it to look up the dirty rows.

Thanks tanathos for the idea.

Donny V.
A: 

Adding arbitrary attributes will mean that your HTML is no longer valid.

If you are using XHTML, however, I think you can add attributes that have a different XML namespace, which won't cause validation problems.

I don't know a great deal about this, but thought I'd mention it as no one else has. Perhaps someone else could expand on, or refute, this if they have a better understanding?

SpoonMeiser
A: 

Can anyone provide a specific example of how adding custom HTML attributes will cause a page to render improperly? All of the comments above that are saying to stay away from it are speculating that it could cause problems down the road. Browsers have and most likely will always ignore attributes that they cannot understand.

Me