Creating Elements, Attributes, and Objects

An important benefit of dynamic content is the ability to create new content from
freshly received data. This is necessary in dynamic menu creation, navigation, breadcrumbs,


and web services, among other applications. Ajax relies on content changing
within the page without having to reload the entire page. To accomplish this, we
need to create new parts of the DOM.
The first method we will concentrate on is createElement( ), which is used to create a
new Element node. An example of this method is:

var element = document.createElement('div');
alert(element.nodeName); /* Alerts 'DIV' */

createElement( ) takes as a parameter the name of the element type to instantiate,
and creates an element of that specified type. It returns an instance of an Element
interface. This is useful as it allows attributes to be directly specified on the returned
element node. In this case, we created a new <div> element and used the variable
element to store that interface.