Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Manipulating the DOM – Creation, | JavaScript Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. JavaScript Course

Lesson 9 of 55

·javascript

Manipulating the DOM – Creation, Insertion & Removal

12 mins

Course progress16%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Use **`DocumentFragment`** when inserting many nodes to avoid multiple reflows.
  • `append`, `prepend`, `before`, `after` are newer, more flexible methods (supported in modern browsers).
  • Always remove event listeners if you remove an element to avoid memory leaks.

Creating elements:

const div = document.createElement('div');

Setting attributes & content:

div.id = 'myDiv';
 div.className = 'card';
 div.textContent = 'Hello';

Appending / inserting:

document.body.appendChild(div);
// insert before an existing node
parent.insertBefore(newNode, referenceNode);

Removing:

div.remove(); // modern
parent.removeChild(div); // legacy
Make it stickPractice what you've learned1 quiz · 7 practical tests · Optional activities from JavaScript CourseBrowse activities
Previous lessonThe Document Object Model (DOM)Continue to lesson 10Event Handling – Adding & Removing ListenersBack to JavaScript Course curriculum