How to Create an HTML Tag in JavaScript on the Fly

If you want to create an html tag in JavaScript on the fly, the good way (ie: without using document.write and innerHTML). Then this is how you do it, use:

document.createElement("tagName");

Where 'tagName' is the tag name (for example: a, script, style, p, h1, h2, h3, input, select, etc...). This will create a tag node that you can then attach to the DOM tree of your document with the following functions:

© Erik Vold 2007-2010. Contact Erik Vold. Top ^