tags:

views:

939

answers:

6

like this: <tag myAttri="myVal" />

+2  A: 

No, this will break validation.

In HTML 5 you can/will be able to add custom attributes. Something like this:

<tag data-myAttri="myVal" />
Developer Art
but, I don't care validation, I just wanna it could be accessed by javascript.
lovespring
It will work of course. But deliberately creating invalid documents is not such a good idea.
Developer Art
Well technically it's not html any more. Equally you could add a load of binary in the middle of a tag - but it won't be html.
Draemon
While you can, you have to ask yourself if there is a better way to accomplish what you need. Why do you need to set custom attributes to access it in Javascript?
Vincent Ramdhanie
Creating invalid documents is a great idea. Google creates them to reduce load times, every site using canvas uses them to create a better user experience, and using javascript frameworks to pull meaningful data off of html elements is much easier using the custom attribute technique.
jfar
Seriously, who cares about making 100% valid HTML.
ChaosPandion
@jfar: Canvas is not invalid. It's not in HTML 4.01; however, it is a perfectly legal part of the upcoming HTML5 specification.
bcat
What do you mean "not invalid"? Its not part of any accepted specification. How can something be valid against a specification that does not exist?
jfar
+4  A: 

Yes, you can, you did it in the question itself: <html myAttri="myVal"/>.

luvieere
Depends on what you define HTML as. I think of HTML as a language based on SGML, with a specific set of elements and attributes. XHTML is a variant on XML, with a specific set of elements and attributes that's a lot like HTML's.When you use your own attributes, it is still SGML of XML, but no longer HTML of XHTML in my opinion.
Douwe Maan
Take it as an adhoc extension, not a standard in a strict sense, but a sort of an implementation of the requirement that it shouldn't fail parsing if it contains custom attributes.
luvieere
DouweM: Of course, there's always the HTML serialization of HTML5, which is neither SGML nor XML.
bcat
And you broke (invalidated) the document in the process. Not good practice.
carillonator
+6  A: 

You can add custom attributes to your elements at will. But that will make your document invalid.

In HTML 5 you will have the opportunity to use custom data attributes prefixed with data-.

Gumbo
Remember "invalid" means nothing. The page will render fine 100% of the time.
jfar
+4  A: 

The jquery data() function allows you to associate arbitrary data with dom elements. Here's an example.

Draemon
+9  A: 

You can amend your !DOCTYPE declaration (i.e. DTD) to allow it, so that the [XML] document will still be valid:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
[
  <!ATTLIST tag myAttri CDATA #IMPLIED>
]>

#IMPLIED means it is an optional attribue, or you could use #REQUIRED, etc.

more info here:

http://www.w3schools.com/DTD/dtd%5Fattributes.asp

carillonator
Need I create a DTD file or just add ATTLIST inline in html file?
lovespring
just put all that at the top of your html file (assuming xhtml 1.0 transitional is ok)
carillonator
A: 

You can set properties from JavaScript.

document.getElementById("foo").myAttri = "myVal"
ewg