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.


createElement()

createElement(tagName) creates in memory an HTML element of the tag specified by tagName.

Syntax:
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 <div> tag, and adds to it a class attribute and content.
var elm = document.createElement('div');
elm.setAttribute('class', 'o_clasa')
elm.innerHTML ='Text in div tag';
console.log(elm);
- This code will create in memory an object that contains the fallowing HTML code:
<div class='o_clasa'>Text in div tag</div></div>

- To add this element in the HTML document structure, you can use the appendChild() method.
In this example, the 'div' element stored in 'elm' is added as the last child in HTML body.
var elm = document.createElement('div');
elm.setAttribute('class', 'o_clasa')
elm.innerHTML ='Div created with createElement(), and added with appendChild()';
document.body.appendChild(elm);

insertBefore()

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

Example, creates a <h3> tag and adds it before a <div> with id='rpr':
<h4>Example with insertBefore()</h4>
<p>If you click the button, it adds a H3 tag created with createElement(), before a DIV with id='rpr'.</p>
<button id='btn1'>Add H3</button>
<div id='rpr'>#rpr, reper.</div>

<script>
function add_h3(){
 // creates a H3 element, class and html content
 var elm = document.createElement('h3');
 elm.className ='o_clasa';
 elm.innerHTML = 'The <b>html text</b> content';

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

 //adds the new element before reper (in parent body)
 document.body.insertBefore(elm, reper);
}

document.getElementById('btn1').addEventListener('click', add_h3);
</script>

Adding dynamically input fields in Form

Here is a practical and useful example of using createElement() and insertBefore() methods to automatically add text fields in a form.
Explanations are in the script code.
<h4>Example adding with insertBefore() input fields in form</h4>
<p>When you click on the 'Add Box' button, the add_input() function creates and adds a text field in form.</p>

<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()' />
</form>

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

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

 // Adds elm
 parinte.insertBefore(elm, reper);
}
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute indicates that an input field must be filled out before submitting?
required="required" autofocus="autofocus" placeholder="text"
<input type="text" name="name1" required="required" />
Which CSS method increases or decreases the size of the HTML element (including its content)?
translate() scale() skew()
#some_id:hover {
  transform: scale(2, 1.5);
  -ms-transform: scale(2, 1.5);    /* IE 9 */
  -moz-transform: scale(2, 1.5);   /* Firefox */
}
Click on the Date object method that returns the day of the month.
setDate() getMonth() getDate()
var rightnow = new Date();
var day = rightnow.getDate();
alert(day);
Which function is used to upload a file on server?
is_file() move_uploaded_file() fopen()
if(move_uploaded_file($_FILES["field_name"]["tmp_name"], "dir/file_name")) {
  echo "The file succesfully uploaded";
}
createElement and insertBefore

Last accessed pages

  1. Wake Up! (3403)
  2. Ajax-PHP Chat Script (37775)
  3. PHP-MySQL Scripts (3924)
  4. Create simple Website with PHP (20532)
  5. Select in MySQL, Output results in HTML Table (5636)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (415)
  2. Read Excel file data in PHP - PhpExcelReader (331)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (257)
  4. PHP getElementById and getElementsByTagName (208)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (199)