Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!