mail us  |  mail this page

products  |  company  |  support  |  downloads  |  isp services  |  contact us

Tech Info - DOM-2 createElement

W3C Core Reference. Part of the Document Interface.

createElement Method

Creates an HTML Element of the defined type, for example, "table". The element is strutural and the various attributes need to be added or in the case of say, a paragraph ("p") a text node (created using createTextNode) is required to contain the visible text. the create lement returns '1' (ELEMENT_NODE) to a nodeType and returns the HTML tag name that was used to create it to nodeName.

Syntax

element = document.createElement(htmltag)

Related Stuff

createTextNode,appendChild

Parameters

Name Description Notes
tag the HTML tag in quotes, for example, "p" = <p> (paragraph) or "div" = <div>.

Examples

// create a new paragraph
newpara = document.createElement("p");

// now some text
sometext = document.createTextNode("what a way to spend a life");

// add the text to the paragraph
newpara.appendChild(sometext);

// get an existing object and append them
existingobject = document.getElementById("one");
existingobject.appendChild(newpara);

The above creates an empty paragraph newpara element and some text sometext which is contained within the paragraph and then appends them (in the order shown) onto the existingobject with id='one'.

Notes:

createElement creates a single instance of the element which can only be used once.

// this code will NOT add two paragraphs with the same contents
// create a new paragraph
newpara = document.createElement("p");
// now some text
sometext = document.createTextNode("what a way to spend a life");
newpara.appendChild(sometext);
// stick the paragraph onto an existing object
obj1 = document.getElementById("one");
obj1.appendChild(newpara);
obj1.appendChild(newpara);

// this code WILL give desired results
// create a new paragraph
newpara = document.createElement("p");
// now some text
sometext = document.createTextNode("what a way to spend a life");
// append to paragraph
newpara.appendChild(sometext);
// stick the paragraph onto an existing object
obj1 = document.getElementById("one");
obj1.appendChild(newpara);
newpara = document.createElement("p");	// create new instance
sometext = document.createTextNode("what a way to spend a life");
newpara.appendChild(sometext);
obj1.appendChild(newpara);

When you create an 'a' (anchor) tag, the text which is anchored is a textnode attached as a childNode to the element and is not an attribute of the anchor.

// create a new anchor
newanchor = document.createElement("a");
// set anchor attributes e.g. href
..
// now create clickable text
sometext = document.createTextNode("click this link");
// add to the anchor
newanchor.appendChild(sometext);
// stick anchor onto an existing object
obj1 = document.getElementById("one");
obj1.appendChild(newanchor);

When you create an element you must append it to something inside the HTML document to give it scope (visibility).

// this code does NOT work
newdiv = document.createElement("div");
// set div attributes 
newdiv.className = "x";
newdiv.id = "mine";
...
mydiv = document.getElementById("mine");	// does not find it

// this code DOES work
newdiv = document.createElement("div");
// set div attributes 
newdiv.className = "x";
newdiv.id = "mine";
document.body.appendChild(newdiv);	// or some other node
...
mydiv = document.getElementById("mine");	// finds it


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.

Tech

tech home
audio stuff
web stuff
dom stuff
css stuff
language stuff
regex stuff
rfc stuff
protocol stuff
cable stuff
lan wiring
rs232 wiring
howto stuff
survival stuff
wireless stuff
ascii codes
data rate stuff
telephony stuff
mechanical stuff
pc stuff
electronic stuff
tech links
open guides
RSS Feed Icon RSS Feed

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 Mozilla

web zytrax.com



HTML Stuff

W3C HTML 4.01
HTML5 (WHATWG)
HTML4 vs HTML5
HTML5 Reference
W3C Page Validator
W3C DOCTYPE

CSS Stuff

W3C CSS1
W3C CSS2.1

DOM Stuff

W3C DOM
W3C DOM Events
Gecko DOM
MSIE DOM

Usability/Access

usability.gov
W3C -WAI
Web Style Guide
Michael L Bernard
WebAim.org

Jolly Useful

Peter-Paul Koch
A List Apart
Eric Meyer on CSS
glish.com
DOCTYPE definitions

Our Stuff

Our DOM Pages
DOM User Guide
DOM Explorer
DOM Navigation
CSS Short Cuts
CSS Techniques
CSS overview
Oh Really!

Javascript

ECMA-262

Pop-out Menus

webmasterbase.com
Brainjar Menubar
Our Lite JS Menus
Our CSS Menus
Tigra Menus

printer friendly

Print Page

SPF Record Conformant Domain Logo

Copyright © 1994 - 2010 ZyTrax, Inc.
All rights reserved. Legal and Privacy
site by zytrax
Hosted by super.net.sg
web-master at zytrax
Page modified: June 21 2007.