How to make quick disabling/enabling of all the elements in any div (inputs, links and jQ Buttons)?
3 Answers
Links do not have a "disabled" property, so you'll have to work a bit harder.
$('#my_div').find(':input').prop('disabled', true);
$('#my_div a').click(function(e) {
e.preventDefault();
});
To re-activate:
$('#my_div').find(':input').prop('disabled', false);
$('#my_div a').unbind("click");
The :input
selector Selects all input, textarea, select and button elements.
-
The true parameter in
prop('disabled')
is redundant. It's not like theattr()
function Commented Aug 5, 2011 at 19:46 -
FYI I just learned a few minutes ago the 'disabled' property does not set as expected with simply
prop('disabled')
whereas checked and selected do.prop('disabled')
should set the property per their documentation. I smell a 1.6.3 bug fix ;) Commented Aug 5, 2011 at 19:58
$('#my_div').find('*').prop('disabled',true);
To re-enable, simply use .removeProp()
http://api.jquery.com/removeProp/
-
1prop('disabled',true) as prop('disabled') just returns value Commented Aug 5, 2011 at 19:54
-
@jacek - good eye. this is a jquery bug, as it behaves as expected with other properties. As you can see in their 1.6 changelog, the expected behavior is to set the property not return the value: blog.jquery.com/2011/05/03/jquery-16-released Commented Aug 5, 2011 at 19:57
-
1
-
find('*')
is more than twice as fast as$('#my_div *')
. Here's some benchmarking evidence: jsperf.com/selector-specific-vs-find Commented Aug 5, 2011 at 20:08 -
To re-enable use $('#my_div').find('*').prop('disabled',false); Commented Sep 20, 2016 at 15:11
$('div').find('input, a, button').prop('disabled', true);
or maybe all:
$('div *').prop('disabled', true);
-
1-1 you should be using
prop()
instead ofattr()
for properties such as "disabled", "checked", "selected", etc. Commented Aug 5, 2011 at 19:41