One of the three basic methods of getting a reference to a node using the DOM. Gets a reference to a node (always an element) which has an id="id-name" attribute in its HTML definition (or more correctly has its id property set to id-name.
element = document.getElementById(id-name)
|id-name||defined by 'id=id-name' in the HTML or has been assigned an id attribute of 'id-name'.
// create some text sometext = document.createTextNode("what a way to spend a life"); // append to an existing object existingobject = document.getElementById("one"); existingobject.appendChild(sometext); // empty HTML paragraph fragment <p id='one'></> // example using id attribute // create a new empty paragraph element newnode = document.createElement("p"); newnode.id = "newnode"; // a long time later node = document.getElementById("newnode"); // do something with the node - create some text sometext = document.createTextNode("add to empty paragraph"); // append it node.appendChild(sometext);
The first example uses an existing HTML id attribute to get the reference to 'one' the second example sets the 'id' attribute by code and then uses this id to obtain a reference later.
Problems, comments, suggestions, corrections (including broken links) or something to add? Please take the time from a busy life to 'mail us' (at top of screen), the webmaster (below) or info-support at zytrax. You will have a warm inner glow for the rest of the day.
If you are happy it's OK - but your browser is giving a less than optimal experience on our site. You could, at no charge, upgrade to a W3C standards compliant browser such as Firefox