Entry
How can I script HTML custom attributes?
Feb 11th, 2000 04:54
Martin Honnen,
Some people like to add custom attributes to HTML elements/tags e.g.
<BODY ACUSTOMATTRIBUTE="Kibology">
However such attributes are not reflected into js in NN2/3/4. IE4/5
reflect them case sensitive so the above is scriptable as
document.body.ACUSTOMATTRIBUTE
while both IE4/5 and NN6 allow script access with the generic
getAttribute
and
setAttribute
methods of elements. Thus
if (document.body && document.body.getAttribute)
var v = document.body.getAttribute('ACUSTOMATTRIBUTE')
is the cross browser way to access such an attribute.
Note that while NN2/3/4 don't reflect such custom attributes into js
they nevertheless allow to add custom properties to every js object
including the ones reflected from the document. So while
<INPUT TYPE="text" NAME="aField" DBKEY="1">
is not reflected into js with NN2/3/4 and thus rather useless it is
possible to add
document.formName.aField.dbKey = 1;
with js.
That works of course with IE too and might be a way to add custom
attributes in a crossbrowser way for older browsers.