The DOM provides the getElementById(tagID) method to access any element with an id attribute, set. That includes head tags, meta tags, script blocks, and any HTML / XHTML defined elements. The tagID is the id attribute or property that's assigned to the tag. If we want to create a named JavaScript variable that represents the element as a JavaScript object, the tag and script code is shown in the code view below.
*Note: the web page renders in a top-down fashion so the element has to be defined and loaded before the script is parsed and understood by the browser otherwise you will get an error message. In the example, the element code is written before the script code section
This is the code view :
First Name <input type="text" value="" name="" id="firstName" />
<script language="JavaScript" type="text/javascript">
var firstField=document.getElementById("firstName");
firstField.name="firstName";
firstField.style.background="lightyellow";
</script>