Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Remove an element if it contains a specific word

I have a button that adds a specific word to a <li> in a <ul>. There is also a button to have a <li> containing a specific word removed.

I really thought includes() or match() would work, but they aren't.

Below is a simplified version of my site. So lastChild or removeChild doesn't actually work in my case, despite being the obvious answer. This is because the <li> being removed may be in a random spot in the <ul>.

I also can't code the specific word like includes(month). It needs to be includes(WORD_BANK[0][0]).

const WORD_BANK = [
    ["day", "1"],
    ["month", "2"],
    ["year", "3"]
];

let counter = 0;

function addTo() {
    document.getElementById("list").innerHTML += `<li>${WORD_BANK[counter][0]}<br>${WORD_BANK[counter][1]}</li>`;
    counter++;
}

function takeOut() {
    document.querySelector("li").innerHTML.includes(WORD_BANK[0][0]).remove();
}
<ul id="list"></ul>
<button id="add" onclick="addTo()">ADD</button>
<button id="remove" onclick="takeOut()">REMOVE</button>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

First, you need querySelectAll to get all li elements, and you need to use find method to find the li which includes the text.

const WORD_BANK = [
    ["day", "1"],
    ["month", "2"],
    ["year", "3"]
];

let counter = 0;

function addTo() {
    if (counter >= 3) {
      console.log('All array has been added');
      return
    };
    document.getElementById("list").innerHTML += `<li>${WORD_BANK[counter][0]}<br>${WORD_BANK[counter][1]}</li>`;
    counter++;
}

function takeOut() {
    const element = [...document.querySelectorAll("li")].find(li => li.textContent.includes(WORD_BANK[counter - 1][0]))
    if (element) {
      element.remove()
      --counter;
    } else {
      console.log('element not exist')
    }
}
<ul id="list"></ul>
<button id="add" onclick="addTo()">ADD</button>
<button id="remove" onclick="takeOut()">REMOVE</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

const WORD_BANK = [
  ["day", "1"],
  ["month", "2"],
  ["year", "3"]
];

let counter = 0;

function addTo() {
  document.getElementById("list").innerHTML += `<li>${WORD_BANK[counter][0]}<br>${WORD_BANK[counter][1]}</li>`;
  counter++;
}

function takeOut() {
  const ele = document.querySelectorAll("li");
  if (ele.length > 0) {
    ele[counter - 1].remove();
    counter--;
  }
}
<ul id="list"></ul>
<button id="add" onclick="addTo()">ADD</button>
<button id="remove" onclick="takeOut()">REMOVE</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

If you really want to do with includes then:

const WORD_BANK = [
  ["day", "1"],
  ["month", "2"],
  ["year", "3"]
];

let counter = 0;

function addTo() {
  if (counter > WORD_BANK.length - 1) return; // check
  document.getElementById(
    "list"
  ).innerHTML += `<li>${WORD_BANK[counter][0]}<br>${WORD_BANK[counter][1]}</li>`;
  counter++;
}

function takeOut() {
  if (counter < 1) return; // check
  --counter;
  Array.from(document.querySelectorAll("li"))
    .find((e) => e.innerHTML.includes(WORD_BANK[counter][0]))
    .remove();
}
    <ul id="list"></ul>
    <button id="add" onclick="addTo()">ADD</button>
    <button id="remove" onclick="takeOut()">REMOVE</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda