Categories
JavaScript Answers

How to Disable Submit Button Only After Submit with JavaScript?

Spread the love

To disable submit button Only after submit with JavaScript, we can set the disabled attribute of the submit button to true after the submit event is emitted.

For instance, we can write:

<form>
  <input type="submit" class="submitBtn" value="I Accept" />
</form>

to create a form.

Then we write:

$(document).ready(() => {
  $("form").submit((e) => {
    e.preventDefault()
    $(".submitBtn").attr("disabled", true);
  });
});

to listen to the submit event on the form with jQuery by calling the submit method.

In the submit event handler, we call e.preventDefault to prevent the default submit behavior.

Then we select the submit button with $(".submitBtn").

Finally, we call attr with 'disabled' and true to disable the submit button.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.