like this: <tag myAttri="myVal" />
views:
939answers:
6
+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
2009-11-14 19:08:16
but, I don't care validation, I just wanna it could be accessed by javascript.
lovespring
2009-11-14 19:09:44
It will work of course. But deliberately creating invalid documents is not such a good idea.
Developer Art
2009-11-14 19:12:58
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
2009-11-14 19:13:59
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
2009-11-14 19:16:44
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
2009-11-14 20:57:02
Seriously, who cares about making 100% valid HTML.
ChaosPandion
2009-11-14 22:18:10
@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
2009-11-14 22:19:28
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
2009-11-15 20:26:01
+4
A:
Yes, you can, you did it in the question itself: <html myAttri="myVal"/>
.
luvieere
2009-11-14 19:11:40
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
2009-11-14 19:57:59
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
2009-11-14 20:15:18
DouweM: Of course, there's always the HTML serialization of HTML5, which is neither SGML nor XML.
bcat
2009-11-14 22:17:14
And you broke (invalidated) the document in the process. Not good practice.
carillonator
2009-11-14 22:30:32
+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
2009-11-14 19:14:50
Remember "invalid" means nothing. The page will render fine 100% of the time.
jfar
2009-11-14 20:54:47
+4
A:
The jquery
data()
function allows you to associate arbitrary data with dom elements. Here's an example.
Draemon
2009-11-14 19:23:30
+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:
carillonator
2009-11-14 22:15:21
just put all that at the top of your html file (assuming xhtml 1.0 transitional is ok)
carillonator
2009-11-15 22:19:44
A:
You can set properties from JavaScript.
document.getElementById("foo").myAttri = "myVal"
ewg
2009-11-14 22:46:14