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 use jQuery to detect window resize events?
- How can I use jQuery to check if an element is visible?
- How do I update to the latest version of jQuery?
- How do I install and use jQuery with NPM?
- How do I prevent XSS attacks when using jQuery?
- How do I get the y-position of an element using jQuery?
- How can I use jQuery in WordPress?
- How can I prevent jQuery XSS vulnerabilities?
- How to use jQuery to achieve a specific task?
See more codes...