Trusted answers to developer questions

How to make a jQuery disable button

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

jQuery is a lightweight JavaScript library that has simplified client-side webpage development by adopting a “write less, do more” ideology.

Sometimes, clicking a button twice ends up submitting the form more than once, which creates a problem. Luckily, it is possible with jQuery to disable the button after the first click.

svg viewer

Library

To use the disable button feature, you need to import this library:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">

Code

In the code below, two buttons are created with the <input> tag. One of the buttons is disabled after the first click, and the other one works as a normal button. In JavaScript, the “disable​” attribute is added to the specific button:

           $("#btn").attr("disabled", true);

RELATED TAGS

jquery
disable
button
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?