I’m new to javascript programming. I have found below example while practicing javascript.
<html>
<head>
<script type="text/javascript">
function changeTabIndex()
{
document.getElementById('1').tabIndex="3"
document.getElementById('2').tabIndex="2"
document.getElementById('3').tabIndex="1"
}
</script>
</head>
<body>
<p><a id="1" href="http://www.w3schools.com">1</a></p>
<p><a id="2" href="http://www.w3schools.com">2</a></p>
<p><a id="3" href="http://www.w3schools.com">3</a></p>
<input type="button" onclick="changeTabIndex()"
value="Change TabIndex" />
</body>
</html>
What’s my doubt is, naming conventions for id attribute must start with an alphabet followed by numbers and underscore. But in this example even though they used numbers as id’s the code working fine.Then what is the need of following naming conventions. It seems to be simple but anyone please clarify it.
Most people would recommend that you don’t rely on http://www.w3schools.com for information regarding Javascript. There are lots of errors in their documentation and their examples. A better source of information is the actual W3C reference docs. Information on IDs can be found at http://www.w3.org/TR/html401/struct/global.html#h-7.5.2. In this particular instance, your browser implementation simply isn’t following the spec 🙂