DEV Community

Cover image for Content Manipulation
Bryan N. Lomerio
Bryan N. Lomerio

Posted on

Content Manipulation

textContent:

Retrieves or sets the text content of an element, excluding any HTML tags.

const element = document.getElementById('myElement');
element.textContent = 'New text content';
Enter fullscreen mode Exit fullscreen mode

innerText:

Similar to textContent, but it takes into account the styling of the element.

const element = document.getElementById('myElement');
element.innerText = 'Updated text';
Enter fullscreen mode Exit fullscreen mode

innerHTML:

Retrieves or sets the HTML content (including tags) of an element.

const element = document.getElementById('myElement');
element.innerHTML = '<p>New paragraph with <strong>HTML</strong> content</p>';
Enter fullscreen mode Exit fullscreen mode

outerHTML:

Retrieves or sets the HTML content of an element, including the element itself.

const element = document.getElementById('myElement');
element.outerHTML = '<div id="newElement">Updated content</div>';
Enter fullscreen mode Exit fullscreen mode

value:

Get or set the value of form elements like or .

const input = document.querySelector('input');
input.value = 'New input value';
Enter fullscreen mode Exit fullscreen mode

setAttribute():

Sets a specific attribute's value on an element.

const element = document.getElementById('myElement');
element.setAttribute('title', 'New tooltip text');
Enter fullscreen mode Exit fullscreen mode

getAttribute():

Retrieves the value of a specified attribute.

const element = document.getElementById('myElement');
const title = element.getAttribute('title');
console.log(title);
Enter fullscreen mode Exit fullscreen mode

removeAttribute():

Removes an attribute from an element.

const element = document.getElementById('myElement');
element.removeAttribute('title');
Enter fullscreen mode Exit fullscreen mode

appendChild():

Appends a new child element to a parent element.

const parent = document.getElementById('parent');
const newElement = document.createElement('div');
parent.appendChild(newElement);
Enter fullscreen mode Exit fullscreen mode

insertBefore():

Inserts a new node before an existing child node.

const parent = document.getElementById('parent');
const newElement = document.createElement('div');
const referenceNode = document.getElementById('existingElement');
parent.insertBefore(newElement, referenceNode);
Enter fullscreen mode Exit fullscreen mode

replaceChild():

Replaces an existing child element with a new one.

const parent = document.getElementById('parent');
const newElement = document.createElement('div');
const oldElement = document.getElementById('oldElement');
parent.replaceChild(newElement, oldElement);
Enter fullscreen mode Exit fullscreen mode

cloneNode():

Creates a copy of an element (can be a deep copy including its child nodes or shallow copy).

const element = document.getElementById('myElement');
const clone = element.cloneNode(true); // true for deep clone
Enter fullscreen mode Exit fullscreen mode

Top comments (0)