jqueryHow do I disable a button using jQuery?
To disable a button using jQuery, you can use the prop() method. This method sets or returns properties and values of the selected elements. To disable a button, you can set the disabled property to true.
$(document).ready(function(){
$("button").click(function(){
$(this).prop("disabled", true);
});
});
The code above will disable the button when it is clicked.
The parts of the code are:
$(document).ready(function(){: This is a jQuery method which allows the code to execute when the DOM is fully loaded.$("button"): This is a jQuery selector which selects all the<button>elements in the DOM..click(function(){: This is a jQuery method which allows you to add an event listener to the selected elements.$(this): This is a jQuery selector which selects the current element..prop("disabled", true): This is a jQuery method which sets thedisabledproperty of the selected element totrue.
Helpful links
More of Jquery
- How do I use jQuery ZTree to create a hierarchical tree structure?
- How do I generate a QR code using jQuery?
- How do I use jQuery to zip files?
- How can I get the y position of an element using jQuery?
- How do I use jQuery to zoom in or out on an element?
- How do I use a jQuery UI Slider?
- How do I create a jQuery Yes/No dialog?
- How can I use JQuery with Yii2?
- How do I create a quiz using the jQuery plugin?
- How do I prevent XSS attacks when using jQuery?
See more codes...