Using element.insertBefore() method in TypeScript

Updated: February 14, 2024 By: Guest Contributor Post a comment


The insertBefore() method in the world of web development is a powerful tool that enables developers to dynamically manipulate the Document Object Model (DOM) to create interactive and engaging user interfaces. TypeScript, a typed superset of JavaScript, enables developers to use this method while benefiting from strong typing and object-oriented principles. This article will explore the use of the insertBefore() method in TypeScript through practical examples, illustrating its utility in various scenarios.

Understanding insertBefore()

In TypeScript, the insertBefore() method is used to insert a new node before a reference node as a child of a specified parent node. The syntax is as follows:

parentNode.insertBefore(newNode, referenceNode);

This method requires two arguments: the node to be inserted (newNode) and the node before which the new node should be inserted (referenceNode). If referenceNode is null, the newNode is added to the end of the list of children of parentNode.

Basic Example

Let’s start with a basic example that inserts a new paragraph before an existing paragraph in an HTML document.

const parentNode = document.getElementById('parent');
const referenceNode = document.getElementById('reference');
const newNode = document.createElement('p');
newNode.textContent = 'This is a new paragraph.';
parentNode.insertBefore(newNode, referenceNode);

After executing the above code, you will see a new paragraph inserted before the referenceNode in your document.

Inserting Multiple Elements

One common scenario is the need to insert multiple elements before a specific node. TypeScript’s type safety and control structures can help manage this task more efficiently.

const elements = [
  'Element 1',
  'Element 2',
  'Element 3'
const parent = document.getElementById('parentDiv');
const refNode = document.getElementById('referenceNode');

for (const elementText of elements) {
  const newElement = document.createElement('div');
  newElement.textContent = elementText;
  parent.insertBefore(newElement, refNode);

This loop will insert three new div elements with text (‘Element 1’, ‘Element 2’, ‘Element 3’) before the refNode.

Working with TypeScript Classes

In more advanced scenarios, you may want to create DOM elements dynamically using TypeScript classes.

class CustomElement {
  element: HTMLElement;

  constructor(tagName: string, text: string) {
    this.element = document.createElement(tagName);
    this.element.textContent = text;

  insertBefore(parentNode: HTMLElement, referenceNode: HTMLElement | null): void {
    parentNode.insertBefore(this.element, referenceNode);

const parent = document.getElementById('parentDiv');
const reference = document.getElementById('referenceDiv');
const customElement = new CustomElement('div', 'Class-based Element');
customElement.insertBefore(parent, reference);

This example demonstrates how to encapsulate the creation and insertion of a DOM element within a TypeScript class, making your code more modular and reusable.


The insertBefore() method is a versatile tool for DOM manipulation, especially when used within TypeScript for added type safety and clarity. Through the presented examples ranging from basic to advanced, you can see how inserting elements dynamically can be seamlessly achieved, enhancing the interactivity and responsiveness of your web applications. Understanding how to effectively use the insertBefore() method in TypeScript can significantly improve the quality and maintainability of your web projects.