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

277
Visualizações
How to change value of Submit form button only if all required fields of the form are filled in (JS)

I want to change the value of submit form button to "Loading..." with JAVA SCRIPT only if all the required fields in the form are filled in. Please help. Thank you!!!

<form>
    <div>
        <input id="my-file" name="my-file" type="file" accept="image/png, image/gif, image/jpeg" required>
    </div>
    <div>
        <input type="email" value="" name="my_email" required>
    </div>
    <div>
        <input id="visible_submit" type="submit" value="Submit">
    </div>
</form>
<script type="text/javascript">
var mySubmit = document.getElementById("visible_submit");
mySubmit.onclick = function addLoading() { document.getElementById("visible_submit").setAttribute("value", "Loading...");};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of listening for the click event on the submit button, listen for the submit event on the form.

Use checkValidity() to check whether the user has filled all the required fields, and Event.preventDefault() to prevent form submission:

var mySubmit = document.getElementById("visible_submit");
const form = document.querySelector('form');
form.addEventListener('submit', function(e) {
  if (form.checkValidity()) {
    document.getElementById("visible_submit").value = "Loading...";
  }
  e.preventDefault();
})
<form>
  <div>
    <input id="my-file" name="my-file" type="file" accept="image/png, image/gif, image/jpeg" required>
  </div>
  <div>
    <input type="email" value="" name="my_email" required>
  </div>
  <div>
    <input id="visible_submit" type="submit" value="Submit">
  </div>
</form>

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