views:

506

answers:

8

Possible Duplicates:
Custom attributes - Yay or nay?
Non-Standard Attributes on HTML Tags. Good Thing? Bad Thing? Your Thoughts?

In current learning project I am working on, I need to add an attribute whose value will be a number. At first I thought of using "id" for this purpose but an answer revealed that it is not good to do that.

Is it OK if I create my own attribute, say "messid" and assign a numeric value such as "12", "6" etc to it?

Here is why I want to do this so that you can correct me if I am doing it totally wrong: I need to access this number in my JavaScript (using jQuery). Just taking the value of attribute is easy but extracting the numeric value from a string like "m12" or "m6" is a pain. (I am a beginner in JavaScript world.)

A: 

No - it's not.

Arnis L.
Haters - what did i say wrong? Is it fine if site does not pass W3 validation?
Arnis L.
I up-voted this *only because* I feel it's a genuine answer; though I'd hazard a guess that the down-votes were due to a lack of explanation. And no, I don't think just providing a link is sufficient. I do believe that you should at least summarise the page you link to, even if you don't choose to explain *yourself*.
David Thomas
@Arnis: May be, may be not, but how hard would it have been for you to include that small amount of text in your "answer"?
AnthonyWJones
@ricebowl, I'm a programmer - Hermant didn't ask for an argument explicitly
Arnis L.
@AnthonyWJones - it wouldn't be hard at all. But then - we wouldn't have this exciting conversation. :)
Arnis L.
Anyway - if this answer is wrong - prove it. I'll be glad to learn something new and delete my answer. :)
Arnis L.
what if the external link breaks? The answer becomes....worthless!
redsquare
@redsquare, no - it wouldn't be worhtless - it would be argumentless.
Arnis L.
@Arnis hmmm, you appear to be a few beans short.
redsquare
@Arnis: This is one of those wars where both sides have a reasonable point. I don't downvote people for having reasonable point even if it conflicts with my own. I downvoted because you have not made (and dispite this converstion still haven't made) any effort at all to express why you said "No." Had you included the simple sentence "because it would faule W3C validation" I would not have downvoted (or removed my down vote).
AnthonyWJones
-1 not very helpful
Tomas
@Tomas whatever....
Arnis L.
+14  A: 

There has been much discussion about this:

At the end of the day, I am on the camp that believes data attributes are the best way to go. They are being introducted in HTML5 to avoid name conflicts. Essentially, if you want to store anything data related you just prepend "data-" on the attribute name:

<div class="user" data-userid="5"></div>

The only con to the whole thing is then that your XHTML won't validate, but I honestly don't care about that stuff. (That's right, I said it)

Paolo Bergantino
+1. I Completely agree this approach is a working solution that is simple to implement and doesn't break anything. Pragmatism over Idealism wins in my book too.
AnthonyWJones
I would go with this, since it works today and since it is in the HTML5 specifications it will probably work 10 years from now. It also keeps the data independent from other values (such as keeping it in the `id` attribute, which might have to change for other reasons later on) while still keeping it on the element. And yes, HTML validation errors should be seen as recommendations/pointers to problems, not must-fix errors (and while on that trail, I'd say the same for JSLint just to stir things up a bit =)
Blixt
"That's right, I said it" -- the first step is admitting it. ;)
nickf
+3  A: 

In HTML 5 you're allowed to add any attribute starting with data-, so e.g. <div data-messid="12"> is OK.

HTML 4 and XHTML 1 won't validate if you add your own attribute, but besides that nothing bad will happen if you choose attribute name unique enough (so it won't conflict with any current or future HTML attribute).

porneL
+1  A: 

I use custom attributes, and since they are supported by all browsers I checked I think it is not bad to use them. You can also use custom HTML tags to simulate HTML5, with some IE hack, so why not use attributes, if they don't need any hacks?

Anyway, you can read similar discussion here: http://stackoverflow.com/questions/992115/custom-attributes-yay-or-nay

Thinker
A: 

This isn't a definitive answer, but having had to do this in the past I can say this not only works well, it is cross-browser friendly.

Nathan Taylor
A: 

see this Data attr

andres descalzo
+2  A: 

Just so you know, you can easily extract an ID from a string like m12 or m6, I would do it like this:

//name the IDs m_12, m_3 etc
var number = $('#someElement').attr('id').split('_')[1];

Or if say, you have a bunch of links with numbers in the ID as above, and all the links have the clickMe class:

$('a.clickMe').click(function() {
    alert($(this).attr('id').split('_')[1]);
});
karim79
That's how I usually do something if I simply have an ID I want to retrieve, but sometimes there's more than that.
Paolo Bergantino
+1  A: 

If using jQuery you can use .data to store custom information against an element.

The downside of custom attributes are:

  • IE6 creates extra objects to store custom 'expando' attributes these have a tendency to leak especially if they are created via script.

  • validation issues

redsquare
I am using ASP.NET MVC and I am trying to add custom attribute "data-messid" using htmlAttribs parameter of html helper methods: new { @class = "mcf", data-dd = Html.AttributeEncode (m.ID)}. But it doesn't work (syntax problem). I see you are active in ASP.NET MVC tag, so please can you help me how to do it?
Hemant
Sure - ask another Q with a asp.net mvc tag
redsquare
although what you have looks correct
redsquare
I thought it will be a too trivial question. Anyway, going to open another question for that...
Hemant