get text inside element javascript code example

Example 1: get text inside element javascript

// get the text inside an element
const text = element.innerText;

// get the html content inside an element
const html = element.innerHTML;

Example 2: textcontent in javascript

// The textContent property sets or returns the text content of the specified node

// read text content from id .. 
let text = document.getElementById('id').textContent;
console.log(text);

// change text content of id .. 
document.getElementById('id').textContent = "text is changed now !";

Example 3: set text of dom element

document.getElementById("myBtn").textContent = 'Hello World';

Tags:

Html Example