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
  • 1,997
  • 9
  • 33
  • 66

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
  • 879,024
  • 165
  • 1,615
  • 1,639
1

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

output.prop('disabled', (input.val().length <= 0));
whitneyit
  • 1,190
  • 6
  • 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,383
  • 5
  • 23
  • 52