1

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?

5
  • Why what's wrong with what you have? It is concise enough. Commented Oct 1, 2011 at 21:00
  • I was hoping for a disabled() function of some kind. Commented Oct 1, 2011 at 21:05
  • No, there isn't such a function, but $('input[type=submit]', this).prop('disabled', true); is clear enough I think. :) Commented Oct 1, 2011 at 21:07
  • Why prop() instead of attr()? Commented Oct 1, 2011 at 21:12
  • I explained it shortly in my answer. If you still don't understand it, please let me know. Commented Oct 1, 2011 at 21:16

4 Answers 4

2

jQuery disable plugin.

$('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();
Sign up to request clarification or add additional context in comments.

7 Comments

A plugin for this trivial task? Common. :)
You should use .prop() for setting the disabled attribute on jQuery > 1.6, not .attr().
Your non-plugin solution is perfect. Thanks.
@Shef: Why do you say "should"? Both prop and attr work equally well.
@arezzo I am not the one who says so, the docs do. Just because something works now, doesn't mean it won't be deprecated in the future.
|
2

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.

Comments

1
$('#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.

2 Comments

Ok. I just read the docs you linked to. How do you know if you're dealing with a property or an attribute. None of the examples mentioned on the docs include disabled. Are you sure it's not an attribute?
@arezzo Yes, they do. I am quoting you from the docs. It's easy to hit CTRL+S to find where the text was quoted from.
0

One cleaner version can be this:

$(this).find(':submit').attr('disabled', true);

also

$(this).find(':submit').prop('disabled', true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.