]> Specifying Custom HTML Attributes With A Custom DOCTYPE
Skip To Main Content | Home | Sitemap | Contact
Latest Visitors: United States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagRussian Federation's flagItaly's flagIndia's flagIndia's flagUnited States's flagFrance's flagGermany's flagIsrael's flagItaly's flagUnited States's flagChina's flagChina's flagUnited States's flagIndia's flagUnited States's flagUnited States's flagChina's flagUnited States's flagRussian Federation's flagChina's flag
 

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.) This page does have custom attributes which are specified in the markup per W3C recommendations using the <!ATTLIST tags and does validate correctly.

One excellent use of this method is to validly add attributes to form elements to be used in form validation.




<!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>Specifying Custom HTML Attributes</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2010 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+='VALUE '+el.value+'\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);
}

function toggleNote() {
    var oEl=document.getElementById('noteLink');
    if (oEl.innerHTML=='[more]') {
        document.getElementById('note').style.display='block';
        oEl.innerHTML='[hide]';
    }else{
        document.getElementById('note').style.display='none';
        oEl.innerHTML='[more]';
    }
}
</script>
<style type="text/css">
#topmenu {
    top:10px;
    z-index:2;
    position:absolute;
    width:96%;
    color:#FFFFFF;
    background-color:#B36B00;
    font-size:80%;
    text-align:right;
    padding-right:15px;
    height:16px;
    voice-family: "\"}\"";
    voice-family: inherit;
    height:15px
}

html>body #topmenu {
    height:15px;
}
</style>
</head>

<body>
  <input type="text" id="t1" name="t1" size="20" value="This has custom attributes" custom1="Value of custom attribute one" extradata="extradata 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>


This Weeks Most Popular Pages Newest Pages