I have the following code to disable a submit button when the form is submitted:
$('#myform').submit(function(){
$('input[type=submit]', this).attr('disabled', 'disabled');
});
Is there a more concise syntax for this?
$('input').disable(); //disables
$('input').enable(); //enables
$('input').toggleDisabled(); //toggles
$('input').toggleEnabled(); //toggles
$('input').toggleDisabled(true); //disables
$('input').toggleEnabled(true); //enables
Or, if you don't want a plugin, you can use this:
jQuery.fn.disable = function() {
$( this ).prop( "disabled", true );
};
$('input[type=submit]', this).disable();
.prop() for setting the disabled attribute on jQuery > 1.6, not .attr().prop and attr work equally well.Well, I'd give it an ID, first of all, otherwise you'll disable every submit button, which may or may not be what you intend.
Other than that, that's about it unless you want to use the disable plugin.
$('#myform').submit(function(){
$('input[type=submit]', this).prop('disabled', true);
});
It can't get more concise than this. It is clean enough. I changed $.attr() with $.prop(), because this is what you should use to set and get values, which change the state of an element, but not the attribute itself.
From the docs:
The
.prop()method should be used to set disabled and checked instead of the.attr()method.
disabled. Are you sure it's not an attribute?
disabled()function of some kind.$('input[type=submit]', this).prop('disabled', true);is clear enough I think. :)prop()instead ofattr()?