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

354
Visualizações
How fix Error: expected false to equal true html

I am learning the basics of HTML. I have the following code:

alert(document.querySelector('div:nth-child(1)').childNodes[0].textContent.trim().length > 0)
<div>
    <label>Name</label><br/>
    <input type="text"><br/>
    <label>Password</label><br/>
    <input type="password">
</div>
<div>
    <input name="gender " type="radio">
    <label>Male</label>
    <input name="gender " type="radio">
    <label>Female</label>
    <input name="gender " type="radio">
    <label>Others</label>
</div>
<div>
    <input name="answer" type="checkbox">
    <label>Answer 1</label>
    <input name="answer" type="checkbox">
    <label>Answer 2</label>
    <input name="answer" type="checkbox">
    <label>Answer 3</label>
    <input name="answer" type="checkbox">
    <label>Answer 4</label>
</div>
<div>
    <input type="submit" value="Submit">
</div>

I expected the result to be true, since the first element of the first <div> is a <label>, which contains a string of non-zero length. The actual result is false, however. Why?

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

0

You are using childNodes, which retrieves all children, including text:

childNodes includes all child nodes—including non-element nodes like text and comment nodes. To get a collection of only elements, use Element.children instead.

Thus, document.querySelector('div:nth-child(1)').childNodes[0] gives you the text that is between the <div> and the <label>, which stripped has no length. Instead, you should use children, which gets the child elements.

For example:

alert(document.querySelector('div:nth-child(1)').children[0].textContent.trim().length>0)
<div>
    <label>Name</label><br/>
    <input type="text"><br/>
    <label>Password</label><br/>
    <input type="password">
</div>
<div>
    <input name="gender " type="radio">
    <label>Male</label>
    <input name="gender " type="radio">
    <label>Female</label>
    <input name="gender " type="radio">
    <label>Others</label>
</div>
<div>
    <input name="answer" type="checkbox">
    <label>Answer 1</label>
    <input name="answer" type="checkbox">
    <label>Answer 2</label>
    <input name="answer" type="checkbox">
    <label>Answer 3</label>
    <input name="answer" type="checkbox">
    <label>Answer 4</label>
</div>
<div>
    <input type="submit" value="Submit">
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

When I used your code it seems that the spaces between <div> and <label>Name</label><br/> counts as text. Because of this document.querySelector('div:nth-child(1)').childNodes[0].textContent.trim().length>0 checks the length of the space. Try and remove the spaces in the HTML and the error should disappear.

var foo = document.querySelector('div:nth-child(1)').childNodes[0].textContent.trim().length > 0;
var bar= document.querySelector('div:nth-child(1)').childNodes[0];

console.log(foo);
console.log(bar);
<div><label>Name</label><br/><input type="text"><br/><label>Password</label><br/><input type="password"></div>
<div>
    <input name="gender " type="radio">
    <label>Male</label>
    <input name="gender " type="radio">
    <label>Female</label>
    <input name="gender " type="radio">
    <label>Others</label>
</div>
<div>
    <input name="answer" type="checkbox">
    <label>Answer 1</label>
    <input name="answer" type="checkbox">
    <label>Answer 2</label>
    <input name="answer" type="checkbox">
    <label>Answer 3</label>
    <input name="answer" type="checkbox">
    <label>Answer 4</label>
</div>
<div>
    <input type="submit" value="Submit">
</div>

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