]> rodsdot.com :: custom attributes
Home|Sitemap|Contact

How can you use custom attributes on an HTML element?

When you add a custom attribute on an element, your page will no longer validate to the DOCTYPE you specified. (If you did not specify a DOCTYPE you do not have valid HTML or XHTML.) [more]



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"
[
  <!ATTLIST input custom1 CDATA #IMPLIED>
  <!ATTLIST input extradata CDATA #IMPLIED>
]>
<html lang="en">

<head>
<title>custom attributes</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005, 2006 Roderick Divilbiss">
<script type="text/javascript">
<!--
function showAtts(el) {
  var out='Attributes for '+el.name+'\n';
  out+='ID '+el.id+'\n';
  out+='TYPE '+el.type+'\n';
  out+='CUSTOM1 '+el.getAttribute('custom1')+'\n';
  out+='extradata  '+el.getAttribute('extradata ')+'\n';
  alert(out);
}

function setAtt(el1,el2) {
  el1.setAttribute('extradata ',el2.value);
  showAtts(el1);
}
//-->
</script>
</head>

<body>
  <input type="text" id="t1" name="t1" size="20" value="This has custom attributes" custom1="Value of custom attribute one" extradata="custom 2 value"> <input type="button" value="Show Value of Custom Attributes" name="B1" onclick="showAtts(document.getElementById('t1'));"><br>
  <input type="text" id="t2" name="t2" size="20"> <input type="button" onclick="setAtt(document.getElementById('t1'),document.getElementById('t2'));" value="Set New Value for custom1" name="b2"></div>
</body>

</html>