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 attribute is used in <a> tag for the address of the link?
src href rel
<a href="" title=""></a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
createElement and insertBefore

Last accessed pages

  1. The Rise Of Atlantis (5142)
  2. Mahjong1 (2548)
  3. Qwop (20722)
  4. Backgammon (5793)
  5. Add data from form in text file in JSON format (7681)

Popular pages this month

  1. Qwop (4728)
  2. Bubbles3 (4343)
  3. Butterfly Kyodai (3092)
  4. Zuma Deluxe (2868)
  5. Backgammon (1464)