Categories: All Free JavaScripts/ Applets Tutorials References

JavaScript Kit > DOM Reference > Here

Document Object Methods

Last updated: Februrary 27th, 2006

Document object methods

Methods Description
createAttribute("attributename") Creates a new attribute, ready to be inserted somewhere in the document. It returns a reference to the created attribute.

Example(s):

var styleattr=document.createAttribute("align")
styleattr.nodeValue="center"
document.getElementById("sister").setAttributeNode(styleattr)

createComment(commenttext) Creates an instance of the comment node. Once created, you can then insert it into the document tree using appendChild(), for example.

Example(s):

var commentnode=document.createComment("Copyright JavaScript Kit")
document.getElementById("mydiv").appendChild(commentnode)

createDocumentFragment() Creates an empty document fragment. The result is a temporary container for creating and modifying new elements or attributes before introducing the final result to your document tree. This is a very useful method when you're performing multiple operations that add to or modify the document tree. Instead of directly modifying the document tree each time (very inefficient), it's much better to use a temporary "whiteboard" that is created by createDocumentFragment() to perform all your operations on first before finally inserting the result to the document tree.

Example(s):

<div id="sister"></div>

<script type="text/javascript">
var docfrag=document.createDocumentFragment()
var mydiv=document.createElement("div")
var divtext=document.createTextNode("This is div text")
mydiv.appendChild(divtext)
docfrag.appendChild(mydiv)
document.getElementById("sister").appendChild(docfrag) //div now reads "this is div text"
</script>

createElement(tagName) Creates an instance of the element object, which can then added to the document tree using appendChild(), for example.

Example(s):

var textblock=document.createElement("p")
textblock.setAttribute("id", "george")
textblock.setAttribute("align", "center")
document.body.appendChild(textblock)

createTextNode(text) Creates a new text node, which can then be added to an element in the document tree.

Example(s):

var headertext=document.createTextNode("Welcome to JavaScript Kit")
document.getElementById("mytitle").appendChild(headertext)

getElementById(id) Accesses any element on the page via its ID attribute. A fundamental method within the DOM for accessing elements on the page.
getElementsByName(name) Returns an array of elements with a name attribute whose value matches that of the parameter's. In IE6, elements with an ID attribute of the matching value will also be included in the array, and getElementsByName() is limited to retrieving form objects such as checkboxes and INPUT. In Firefox, nither of these "pitfalls" apply.

<div name="george">f</div>
<div name="george">f</div>

<script type="text/javascript">
var georges=document.getElementsByName("george")
for (i=0; i< georges.length; i++)
// do something with each DIV tag with name="george". Firefox only.
</script>

getElementsByTagName(tagname) Returns an array of elements whose tag name matches the parameter. In Firefox/ IE6+, you may enter an asterisk ("*") as the parameter to retrieve a list of all elements within the document.

var ptags=document.getElementsByTagName("p")
var alltags=document.getElementsByTagName("*") //returns all elements on page

Note: See also JavaScript document object.

Sponsored By

DD CSS Library
Free CSS menus and codes to give your site a visual boost.

Daniweb JavaScript Community

DOM Window
DOM Document
DOM Element
DOM Event
DOM Style
DOM Table
Miscellaneous

CopyRight © 1998-2008 JavaScript Kit. NO PART may be reproduced without author's permission.