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?
- Check if input has focus
- How do I use jQuery to zip files?
- How do I download a zip file using jQuery?
- How do I add a zoom feature to my website using jQuery?
- How can I use jQuery and React together in my software development project?
- How do I use the jQuery window load function?
- Include latest jQuery library version into HTML
- How can I use jQuery to zoom an image when the user hovers over it?
- How do I use jQuery to change the z-index of an element?
See more codes...