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 thedisabled
property of the selected element totrue
.
Helpful links
More of Jquery
- How do I use jQuery ZTree to create a hierarchical tree structure?
- 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 get the y-position of an element using jQuery?
- How can I use JQuery with Yii2?
- How do I download a zip file using jQuery?
- Check if input has focus
- How do I prevent XSS attacks when using jQuery?
- How do I install and use jQuery with NPM?
- How to use jQuery to achieve a specific task?
See more codes...