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

204
Visualizações
How can I check if a specific value exists in a list?

I am trying to check the value of all li tags in a ul list. If any list item contains the value of "None" then it must add specific text into a div. If no li tag contains the value of "None" then different text must be added to the div.

I'm getting the following error in the console:

Uncaught TypeError: can't access property "includes", mylist.textContent is undefined

So it seems that I'm not using the .includes() method correctly.

var mydiv = document.querySelector("#mydiv");
var mylist = document.querySelectorAll("#mylist li");
var myresult = mylist.textContent.includes("None");

if (myresult == true) {
  mydiv.textContent = "Result is True";
} else {
  mydiv.textContent = "Result is False";
}
My HTML:

<ul id="mylist">
  <li>item 1</li>
  <li>item 2</li>
  <li>None</li>
  <li>item 4</li>
  <li>item 5</li>
  <li>item 6</li>
  <li>item 7</li>
</ul>

<ul id="mydiv">
</ul>

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

0

querySelectorAll returns a list of elements, so you cannot check textContent of a particular element

I'd suggest that you should convert your list to an array with a spreading operation (to be able to use array functions) and then use some to check your condition

var mydiv = document.querySelector("#mydiv");
var mylist = document.querySelectorAll("#mylist li");
var myresult = [...mylist].some(item => item.textContent.includes("None"));

if (myresult === true) {
  mydiv.textContent = "Result is True";
} else {
  mydiv.textContent = "Result is False";
}
<ul id="mylist">
  <li>item 1</li>
  <li>item 2</li>
  <li>None</li>
  <li>item 4</li>
  <li>item 5</li>
  <li>item 6</li>
  <li>item 7</li>
</ul>

<ul id="mydiv">
</ul>

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