tags:

views:

46

answers:

1

I can't for the life of me figure out how you would do the following:

$("select").change(function() {
  var cb = $(this).parent().next().find("input[type=checkbox]");
  $(this).val().length > 0 ? cb.attr('checked', true) : cb.attr('checked', false) 
});
$("input[type=text]").keyup(function() {
  var cb = $(this).parent().next().find("input[type=checkbox]");
  $(this).val().length > 0 ? cb.attr('checked', true) : cb.attr('checked', false) 
});

As one function. Really don't like repetition, especially when they're doing the exact same thing.

My jQuery jedi skills aren't tailored to this scenario.

+1  A: 

You first declare the function:

function myFunc() {
   var cb = ...
}

and then add the assignments:

$('select').change(myFunc);
$('input[type=text]').keyup(myFunc);
David Hedlund
All too easy. I failed because I was doing `$('select').change(myFunc())` I believe. Thanking you squire!
Kezzer
ah, yes. that would execute `myFunc` immediately and try to pass the *return value* of myFunc to the change listener.
David Hedlund
Good to know the reason, thanks very much for that :)
Kezzer