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

198
Visualizações
What actually happens when a method is the condition of an if statement
if (username.value.length >= 8) {

       msg.style.background = "green"
       msg.appendChild(validMsg)

       if (msg.appendChild(invalidMsg)) {
       msg.removeChild(invalidMsg)
       }

   }

msg references a div

validMsg and invalidMsg are both text nodes, created via createTextNode

This is being used to add a success message and to remove a warning message on a page, if the warning message is present.

It works without the nested condition, but returns an error ('The node to be removed is not a child of this node')

I have other solutions, but would like to know specifically what happens with the nested if. I know an if can be used to check for JavaScript features, but in the case above, is it actually executing the appendChild method and then removing the child, or just checking if it can appendChild?

A better solution using the approach above would be appreciated also.

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

0

It looks like you are trying to message a user if the content of a form input is OK. Here are two examples. On using CSS and the other CSS and JavaScript.

span.invalid, span.valid {
  display: none;
}

input:invalid ~ span.invalid {
  display: inline;
}

input:valid ~ span.valid {
  display: inline;
}
<form name="form01">
  <label>Username: <input type="text" name="username" minlength="8" required/>
    <span class="valid">OK</span>
    <span class="invalid">not OK</span>
  </label>
  <button>Submit</button>
</form>

document.forms.form01.addEventListener('submit', e => {
  e.preventDefault();
  console.log("submit");
});

document.forms.form01.addEventListener('invalid', e => {
  e.preventDefault();
  e.target.parentElement.querySelector('.invalid').classList.add('show');
}, true);

document.forms.form01.addEventListener('input', e => {
  e.target.parentElement.querySelector('.invalid').classList.remove('show');
}, false);
span.invalid {
  display: none;
}

span.show {
  display: inline;
}
<form name="form01">
  <label>Username: <input type="text" name="username" minlength="8" required/>
    <span class="invalid">not OK</span>
  </label>
  <button>Submit</button>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

Thanks for the replies.

I tested it again and removed the code between the curly braces, namely:

 msg.removeChild(invalidMsg)

This results in both the valid and invalid messages appearing on the page, so

 if (msg.appendChild(invalidMsg))

is adding the invalidMsg. The removeChild method is then taking it away again.

It all works exactly the same if like this:

   if (username.value.length >= 8) {

        msg.style.background = "green"
        msg.appendChild(validMsg)

        msg.appendChild(invalidMsg) 
        msg.removeChild(invalidMsg)
    }
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