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

200
Views
How to add or remove specific element from the HTML DOM

I am having this confusion from a long time now, I want to add a specific element like the span in the example between the h1 and h2 tags, but when I click on the button, it added them as the last child of the container, I want to add at the middle of h1 and h2 tags, And on the remove button, I want to remove that specific element only instead of remove all elements, how can I remove that specific element when clicked on the button

const container = document.querySelector('.container');
const btnAdd = document.querySelector('.btnAdd');
const btnRemove = document.querySelector('.btnRemove');

btnAdd.addEventListener('click', () => {
  container.innerHTML += `<span>Remove or Add me</span>`;
})

btnRemove.addEventListener('click', () => {
  container.innerHTML += ``;
})
<div class="container">
  <h1>Element</h1>
  
  <h2>Element</h2>
</div>

<button class="btnAdd">Add</button>
<button class="btnRemove">Remove</button>

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

0

You need a few more lines of code to create a span element and work with it in the event handlers:

const container = document.querySelector('.container');
const btnAdd = document.querySelector('.btnAdd');
const btnRemove = document.querySelector('.btnRemove');
const h2 = document.querySelector('h2');
let addClicked = false;
const newNode = document.createElement('span');

btnAdd.addEventListener('click', () => {
  if(addClicked) {
    return;
  }
  newNode.innerHTML = 'Remove or Add me';
  container.insertBefore(newNode, h2);
  addClicked = true;
})

btnRemove.addEventListener('click', () => {
  newNode.remove();
  addClicked = false;
})
<div class="container">
  <h1>Element</h1>
  
  <h2>Element</h2>
</div>

<button class="btnAdd">Add</button>
<button class="btnRemove">Remove</button>

about 4 years ago · Juan Pablo Isaza Report

0

The other answer only lets you add/remove one single element.

I've updated your snippet as you can run it below - this lets you keep adding elements (by first creating a span element with javascript and then using the insertBefore method on the h2 element to insert it dynamically).

The remove button then always deletes the last element that was added, allowing for a more dynamic list.

You can take this one step further and add a delete button within each span, that upon being clicked, will delete any element wherever they are.

const container = document.querySelector('.container');
const btnAdd = document.querySelector('.btnAdd');
const btnRemove = document.querySelector('.btnRemove');
const lastElement = document.getElementById('last-element');

btnAdd.addEventListener('click', () => {
  const newSpan = document.createElement('span');
  newSpan.innerHTML = 'Remove or add me';
  container.insertBefore(newSpan, lastElement);
})

btnRemove.addEventListener('click', () => {
  const lastSpan = Array.from(document.querySelectorAll('.container > span')).pop();
  
  if (lastSpan) {
    container.removeChild(lastSpan);
  }
})
<div class="container">
  <h1 id="first-element">Element</h1>
  
  <h2 id="last-element">Element</h2>
</div>

<button class="btnAdd">Add</button>
<button class="btnRemove">Remove</button>

about 4 years ago · Juan Pablo Isaza Report

0

  • Add empty tag between h tags
  • Use textContent instead of innerHTML. Because it's more safe and fast than innerHTML
    cf. differences between innerHTML and textContent

const btnAdd = document.querySelector('.btnAdd');
const btnRemove = document.querySelector('.btnRemove');
const text = document.querySelector('.text');

btnAdd.addEventListener('click', () => {
  text.textContent = 'Remove or Add me'
})

btnRemove.addEventListener('click', () => {
  text.textContent = '';
})
<div class="container">
  <h1>Element</h1>
  <span class="text"></span>
  <h2>Element</h2>
</div>

<button class="btnAdd">Add</button>
<button class="btnRemove">Remove</button>

EDIT

If you can't define element in html, you can try this code instead.

const container = document.querySelector(".container");
const h1 = document.querySelector(".container > h1");
const btnAdd = document.querySelector(".btnAdd");
const btnRemove = document.querySelector(".btnRemove");

btnAdd.addEventListener("click", () => {
  h1.insertAdjacentHTML("afterend", "<span>Remove or Add me</span>");
});

btnRemove.addEventListener("click", () => {
  const text = document.querySelector("h1 + span");
  container.removeChild(text);
});
<div class="container">
  <h1>Element</h1>
  <h2>Element</h2>
</div>

<button class="btnAdd">Add</button>
<button class="btnRemove">Remove</button>

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!