mail us  |  mail this page

contact us
training  | 
tech stuff  | 

Tech Stuff - DOM-2 createTextNode

W3C Core Reference. Part of the Document Interface.

createTextNode Method

Creates a single instance of a new text node. Text nodes return '3' (TEXT_NODE) to the nodeType and '#text' to nodeName attributes. The text contents are read and written using the nodeValue attribute. Used for any visible text, for example, with a paragraph tag ("p"), or "pre" tag, clickable text in an anchor ("a") tag or raw text in say, a table cell ("td"). In all cases it must be appended (appendChild) to a pre-existing structural element or one created with createElement method.

Syntax

node = document.createTextNode(textstring)

Related Stuff

createElement, appendChild

Parameters

Param Description
textstring the required text as a quoted string for example, "useful text"

Examples

// create a new paragraph
newpara = document.createElement("p");
// now some text
sometext = document.createTextNode("what a way to spend a life");
// stick them onto an existing object
existingobject = document.getElementById("one");
existingobject.appendChild(newpara);
existingobject.appendChild(sometext);

We create an empty paragraph newpara element and some text sometext and appends them (in the order shown) onto the existingobject which has id="one".

Notes:

createTextNode creates a single instance of the text which can only be used once.

// this code will NOT give desired results
// create a text node
// note: snippet assumes obj1 and obj2 are known to be para nodes
sometext = document.createTextNode("what a way to spend a life");
// stick same text onto existing objects - DOES NOT WORK
obj1 = document.getElementById("one");
obj2 = document.getElementById("two");
obj1.appendChild(sometext);
obj2.appendChild(sometext);

// this code WILL give desired results
// note: snippet assumes obj1 and obj2 are known to be para nodes
// now create text
sometext = document.createTextNode("what a way to spend a life");
// add text onto existing objects
obj1 = document.getElementById("one");
obj2 = document.getElementById("two");
// add first instance to obj1
obj1.appendChild(sometext);
// create a new instance an add to obj2
sometext = document.createTextNode("what a way to spend a life");
obj2.appendChild(sametext);


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 Stuff

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

Search

web zytrax.com

Share

share page via facebook tweet this page

Page

email us Send to a friend feature print this page Decrease font size Increase font size Display full width page

Resources

HTML Stuff

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

CSS Stuff

W3C CSS2.1
W3C CSS2.2
Default Styles
CSS3 Selectors
CSS 3 Media Queries
CSS 3 Colors

DOM Stuff

W3C DOM
W3C DOM 3 Core
W3C 3 Events

Accessibility

usability.gov
W3C - WAI
Web Style Guide
WebAim.org

Useful Stuff

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

Our Stuff

Our DOM Pages
DOM Navigation
Liquid Layout
CSS Short Cuts
CSS overview
CSS One Page

Javascript Stuff

ECMA-262

Site

CSS Technology SPF Record Conformant Domain
Copyright © 1994 - 2017 ZyTrax, Inc.
All rights reserved. Legal and Privacy
site by zytrax
Hosted by super.net.sg
web-master at zytrax
Page modified: October 21 2015.