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,327
  • 10
  • 40
  • 73

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
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

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

output.prop('disabled', (input.val().length <= 0));
whitneyit
  • 1,226
  • 8
  • 17
  • 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
  • 12,163
  • 5
  • 35
  • 59