First, the fiddle.
$('#enableButtonB').click(function (e) {
if($(e.target).is(':checked'))
{
$('#myButtonB').removeProp('disabled');
alert('Enable Button B');
}
else
{
$('#myButtonB').prop('disabled', true);
alert('Disable Button B');
}
});
I'm trying to use jQuery's .prop()
and removeProp()
methods to enable and disable a button based on some criteria. It seems to work fine until removeProp()
is called on the element. After that any subsequent calls to prop()
fail to disable to button.
What's the proper way to enable and disable an element repeatedly?
How to turn off toggle in jquery? find(“:input[type='text']”) // Find text elements in that row. . attr('disabled',false). toggleClass('disabled') // Enable them. .
You can use $(":disabled") to select all disabled items in the current context. To determine whether a single item is disabled you can use $("#textbox1").is(":disabled") . Save this answer.
The prop() method sets or returns properties and values of the selected elements. When this method is used to return the property value, it returns the value of the FIRST matched element. When this method is used to set property values, it sets one or more property/value pairs for the set of matched elements.
prop() method provides a way to explicitly retrieve property values, while . attr() retrieves attributes. For example, selectedIndex , tagName , nodeName , nodeType , ownerDocument , defaultChecked , and defaultSelected should be retrieved and set with the . prop() method.
Instead of
.removeProp('disabled')
use
.prop('disabled',false)
Fiddle: http://jsfiddle.net/kqnZz/6/
First, http://jsfiddle.net/iambriansreed/KxGVa/
The jQuery docs are great. removeProp
says:
Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use
.prop()
to set these properties tofalse
instead.
Change:
.removeProp('disabled')
...to...
.prop('disabled', false)
...and...
.prop('disabled', 'disabled')
...to...
.prop('disabled', true)
Try if this short function solves your needs:
$("#enableButtonB").click(function(){
$("#myButtonB").prop('disabled', function (_, val) { return ! val; });
});
Creds to user Explosion Pills for that sweet function :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With