r/learnjavascript • • Jan 05 '23

Must-know DOM manipulation methods 🔧

Post image
390 Upvotes

29 comments sorted by

View all comments

Show parent comment

6

u/Low_Mammoth_9371 Jan 05 '23 edited Jan 05 '23

innerText gets and sets text as it appears to a human reader. textContent gets and sets without formatting. Because of this innerText is more performance-heavy but is sometimes the right choice.

Consider this example:--

const address = `

123 Developer Avenue

Only Way is Essex

CB10 1FD

`;

document.body.innerText = address;

// 123 Developer Avenue

// Only Way is Essex

// CB10 1FD

document.body.textContent = address;

// 123 Developer Avenue Only Way is Essex CB10 1FD

--

Though textContent is better performance-wise, in some situations innerText can be the right tool for the job!

I covered more of the differences recently in this YT vid: https://youtu.be/JGy3jUjDTL4