0

I am looking for a way to clean/condense/improve the performance of my current snippet: input & output are already defined variables.

if (input.val().length <= 0) {
    output.attr('disabled', true);
} else {
    output.attr('disabled', false);
}
O P
  • 2,167
  • 10
  • 36
  • 71

3 Answers3

3

Well, you can use the boolean expression directly:

output.attr('disabled', input.val().length <= 0);

Gratuitous Live Example | Source

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
1

You should be using prop over attr. See here http://api.jquery.com/prop/

output.prop('disabled', (input.val().length <= 0));
whitneyit
  • 1,220
  • 7
  • 16
  • It doesn't matter, the jQuery team *immediately* backed off making `attr` only work on attributes as soon as 1.6 came out. (And of course, `disabled` **is** an attribute, through if we were being very strict, we wouldn't use `true` and `false` with it.) `attr` works just fine for `disabled` and that's not likely to change. – T.J. Crowder Mar 24 '13 at 10:37
0
output.attr('disabled', (input.val().length <= 0));
Manish Mishra
  • 11,823
  • 5
  • 27
  • 57