Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

366
Views
How to append a child div that wraps the inside content

I'm trying to using pure JS (not jquery) to add a <del> insides the <div> that wraps the text 'abc'

For practice purpose, i'm not allowed to innerHTML method, inline CSS, or jquery etc. plain js.

<html>
<div class='todo'>abc</div>
</html>

my attempt.

const todo = document.querySelector('.todo')
const del = document.createElement('del')
todo.appendChild(del) 

But this ended up with <div class='todo'>abc <del></del></div>. I'd like to have <div class='todo'><del>abc</del></div>.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can do a short trick to achieve this:

1) Add textContent of todo to del's textContent.

del.textContent = todo.textContent;

2) Remove the textContent of todo.

todo.textContent = "";

3) Append del as a child.

todo.appendChild(del)

const todo = document.querySelector('.todo')
const del = document.createElement('del')
del.textContent = todo.textContent;
todo.textContent = "";
todo.appendChild(del)
<div class='todo'>abc</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can use the innerText method

const todo = document.querySelector('.todo')
const del = document.createElement('del')
del.innerText = todo.innerText
todo.innerText = ""
todo.appendChild(del) 

about 4 years ago · Juan Pablo Isaza Report

0

Move the content - saves blanking it

const todo = document.querySelector('.todo')
const del = document.createElement('del')
del.append(todo.firstChild)
todo.appendChild(del)
<div class='todo'>abc</div>

Or simpler

const todo = document.querySelector('.todo')
todo.innerHTML = todo.textContent.strike()
<div class='todo'>abc</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!