How to enable/disable a button using jQuery ?
Last Updated :
30 Jul, 2024
Enabling/disabling a button using jQuery involves altering its functionality to either accept user input or not. This is typically done by manipulating its 'disabled' property or attribute through jQuery methods like `.prop()` or .attr().
To enable/disable a button using jQuery, you'll need a basic HTML structure along with JavaScript/jQuery. Below is a simple code structure to get you started
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Enable/Disable</title>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
</head>
<body>
<button id="myButton">Click Me</button>
<script>
$(document).ready(function(){
// jQuery code to enable/disable button
// Add your code here
});
</script>
</body>
</html>
Examples of enabling/disabling a button using jQuery
1. Using the .prop() method:
Using .prop() in jQuery to enable/disable a button involves setting its 'disabled' property to true or false, providing efficient control over its interactivity based on application requirements and user interactions.
Example: In this example we are using jQuery: Toggles button's 'disabled' property on click and double-click events for smooth user interaction using .prop() method.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
</head>
<body>
<h3>Using the .prop() method</h3>
<button id="update">update me</button>
<div style="margin-top: 50px">
<button id="change">click me</button>
</div>
<script>
$("#change").on("click", function () {
$("#update").prop(
"disabled",
true
);
});
$("#change").on(
"dblclick",
function () {
$("#update").prop(
"disabled",
false
);
}
);
</script>
</body>
</html>
Output:
.prop() method Example Output2. Using the .attr() method
Using the .attr() method in jQuery, you toggle the 'disabled' attribute of elements. Clicking a button disables another button, while double-clicking re-enables it, enhancing user interaction and functionality within web pages.
Example: In this example we are using .attr() method to enable/disable button. Clicking disables another button; double-clicking re-enables it, enhancing user interaction and web functionality.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
</head>
<body>
<h3>Using the .attr() method</h3>
<button id="update">Update Me</button>
<div style="margin-top: 50px;">
<button id="change">Click Me</button>
</div>
<script>
$("#change").on("click", function () {
$("#update").attr("disabled", "disabled");
});
$("#change").on("dblclick", function () {
$("#update").removeAttr("disabled");
});
</script>
</body>
</html>
Output:
.attr() method Example output
Similar Reads
How to Disable a Button in jQuery UI ? To disable a button in jQuery UI, we will be using disable() method which is discussed below: jQuery UI disable() method is used to completely disable the button. It returns the button element completely to its initial state. Syntax: $(".selector").button("disable") Parameters: This method does not
2 min read
How to make a Disable Buttons using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making a Disable Button using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet
1 min read
How to disable or enable form submit button in jQuery? Enabling or disabling the form submit button based on a checkbox selection in jQuery. This ensures user compliance with terms and conditions before form submission. Syntax:$('#enabled').click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button').removeAttr('disabled'); } else
2 min read
jQuery UI Buttonset enable() Method jQuery UI consists of GUI widgets, visual effects, and themes implemented using HTML, CSS, and jQuery. jQuery UI is great for building UI interfaces for the webpages. The jQuery UI buttonset widget is used to give a visual grouping for a group of related buttons. The jQuery UI buttonset enable() met
1 min read
jQuery UI Button disable() Method jQuery UI consists of GUI widgets, visual effects, and themes implemented using HTML, CSS, and jQuery. jQuery UI is great for building UI interfaces for the webpages. The jQuery UI Button disable() method is used to disable the button widget. It does not accept any parameter. Syntax: $(".selector").
1 min read