createElement() and insertBefore() are two JavaScript methods used to add new HTML elements in the web page, these elements are created dynamically in memory and added by JavaScript.

1. createElement()

createElement(tagName) creates in memory an HTML element of the tag specified by tagName.
var new_tag = document.createElement(tagName);
- tagName is the name of the tag that will be created.
- new_tag is the variable that stores the object of the created element.
For example, the code below creates a <h3> tag.
<script type="text/javascript"><!--
var new_h3 = document.createElement('h3');

Once this element is created, we can add attributes and content, using "setAttribute()" (or "ClassName" for class) and "innerHTML" for HTML content (can also contains HTML tags).
In the next example is created a <div> tag with class="cls1" and a text content.
<script type="text/javascript"><!--
var new_div = document.createElement('div');
new_div.className = "cls1";
new_div.innerHTML = '<u></u>, JS lesson';
- This code will create in memory an object that contains the fallowing HTML code:
<div class="cls1"><u>Web</u>, JS lesson</div>
- Finally, to attach this <div> element to the HTML document structure, use DOM node method appendChild()
<script type="text/javascript"><!--
- The <div> element stored in "new_div" is added as the last child in HTML body element.

2. insertBefore()

insertBefore() adds a child node in front of another child, both inside a parent element.
parent.insertBefore(newElm, reference);
- Inserts newElm before reference inside the parent.

Example, creates a <h3> tag and adds it in the Body, before a <div> with id="idiv":
<script type="text/javascript"><!--
function add_h3() {
  // creates a H3 element, class and html content
  var new_h3 = document.createElement('h3');
  new_h3.className = 'cls';
  new_h3.innerHTML = 'The <i>html text</i> content';

  // gets the reference tag
  var reference = document.getElementById('idiv');

  // add 'new_h3' before 'reference', inside body
  document.body.insertBefore(new_h3, reference);
<div id="idiv">The reference element</div>
<button onclick="add_h3()">Add H3</button>
- Click the button "Add H3" to see the result.
The reference element
- The parent element can be any other HTML tag, but first you have to get an object reference to it, using the syntax:
var parnt = document.getElementById('parent_id');

3. Adding dynamically text-boxes in Form

Here is a practical and useful example of using createElement() and insertBefore() methods to automatically add text boxes in a form.
Explanations are in the script code.
<script type="text/javascript"><!--
// -

// creates an input element and adds it before the Submit button
function add_input() {
  // sets a new input element input, having the attributes: type=text si name=nume[]
  var new_input = document.createElement("input");
  new_input.setAttribute("type", "text");
  new_input.setAttribute("name", "nume[]"); = 'block';             // sets cssstyle display:block;

  // sets the objects for reference and parent
  var reper = document.getElementById('submit');
  var parinte = reper.parentNode;

  // Adds new_input
  parinte.insertBefore(new_input, reper);
<form action="">
  <input type="text" name="nume[]" />
  <input type="submit" value="Submit" id="submit" /><br /><br />
  <input type="button" value="Add box" onclick="add_input()" />
- When you click on the "Add Box" button, the add_input() function creates and adds a text box.

Daily Test with Code Example

Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
Click on the "echo" correct instruction.
echo "" echo ""; echo """;
echo "Address URL:";
createElement and insertBefore

Last accessed pages

  1. Puzzle Games (13882)
  2. Drag Racer V3 (34933)
  3. Bubble Spinner 2 (881)
  4. Flash Games - Free online Games (29735)
  5. Gravitee (748)

Popular pages this month

  1. Flash Games - Free online Games (1696)
  2. Qwop (1372)
  3. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1218)
  4. Tunnel Rush (1170)
  5. Drag Racer V3 (881)