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 do I use jQuery to zip files?
- How do I download a zip file using jQuery?
- How do I use jQuery to zoom an image when it is clicked?
- How do I use jQuery to change the z-index of an element?
- How do I use jQuery to zoom in or out on an element?
- How do I use jQuery with Yarn?
- How do I use the jQuery hasattr function?
- How do I get the y-position of an element using jQuery?
- How can I get the y position of an element using jQuery?
See more codes...