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

215
Visualizações
Why is the value of my password field always an empty string?

I've been working on my javascript skills lately and the password validation has given me a lot of trouble. The last thing that is stopping me is that the password field always has a value of an empty string for some reason.

It always logs out an empty string, no matter what i put in the input field. I've tried a different browser but that did not have an effect.

<input type="password" id="password">
<button type="submit" onclick="submitIt()">
Submit
</button>

js

const passWord = document.getElementById('password').value

function submitIt() {
console.log(passWord)
}

Here is the jsfiddle: https://jsfiddle.net/k4owfLzn/3/

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

0

Your value is always empty because the value of passWord is set on the loading of page. It is not updated after a user types in a value.

If you modify the code so it is like this you should get the value.

function submitIt() {
  const passWord = document.getElementById('password').value;

  console.log('passWord: ', passWord)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

const passWord

was declared outside the function. I am sure that if you insert it inside the function, you will have the result you were looking for. :)

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