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

264
Visualizações
Unable to disable button on form submit (javascript, html, css)

I am trying to disable a button after it is clicked, but it is not holding its disabled tag. Another weird thing is that if I click the button twice it will disable. Code below

const [loading, setLoading] = useState('Submit');
...

<form onSubmit={(event) => {
    event.preventDefault();
    submitBet(units, line, team, gameID);
    }}> 
    ...
    <button type='submit' className='submit-betslip' id='submit-button-id'>{loading}.</button>
</form>

The loading variable is a useState. The onSubmit function:

    const submitBet = async (units, line, team, id) => {
        if (Number(units) === 0 && Number(line) === 0) {
          console.log('Empty input');
          return
        }
        try {
          document.getElementById('submit-button-id').disabled = true;
        }
        ...
        }

Not sure if it matters but the form is within a React function. Any thoughts?

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

0

Here is a simple example to what you want to do and it's working perfectly.

import React from "react";
import "./styles.css";

export default function App() {
  const [loading, setLoading] = React.useState("Submit");
  const handleSubmit = (e) => {
    e.preventDefault();
    setLoading("Submitting..");
    setTimeout(() => {
      alert("submitted Successfully");
      setLoading("Submit");
    }, 3000);
  };
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <form onSubmit={handleSubmit}>
        <input id="f1" name="f1" />
        <button type="submit" disabled={loading !== "Submit"}>
          {loading}.
        </button>
      </form>
    </div>
  );
}

Simply link the disable prop to the loading state.

If that is not what you want to do, just share the component with us, and I would love to help.

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