views:

270

answers:

3

i have a time dropdown that shows hour in one select, minutes in another and AM/PM in a third. I have a checkbox that says "All day"

i want it so when the user checks on "All day" all of the select dropdowns become disabled.

A: 
$('#allday').toggle(
    function(){
     $('#hour, #minutes, #am-pm').attr('disabled', 'disabled');
    },
    function(){
     $('#hour, #minutes, #am-pm').removeAttr('disabled');
    }
);
Josh Leitzel
A: 

Try this.

$('#checkbox').change(function(){
    $('#Hour, #Minute, #Period').attr('disabled', this.checked);    
});
ChaosPandion
I don't think onchange can be used on checkboxes. At least this didn't work when I tested it. Or if it can be used, I don't know that it would work here, since the "value" of the checkbox isn't technically changing.
Josh Leitzel
The new edit I made actually works.
ChaosPandion
Interesting. Didn't know that you could use onchange on anything but a select box. But I suppose that's reasonable considering the jQuery docs say that it's when a control changes value. Still a little confusing in this instance. Also, your code is only one-way. What if the user de-selects #checkbox?
Josh Leitzel
Contrary to popular belief you can set disabled equal to a Boolean value.
ChaosPandion
Whoops, nvm. Guess you covered that with the this.checked bool. :p
Josh Leitzel
+2  A: 

The trouble with checkboxes is principally getting them to work right in IE6, since the change event fires at all manner of unhelpful times.

Let's start with the function to do the disabling:

function onCheckChange() {
  if ($("#all-day-checkbox").is(':checked'))
    $("select").attr('disabled', 'disabled');
  else
    $("select").removeAttr('disabled');
}

Note that I here use just select which is almost certainly more broad than you'd want, but select.some-class-to-identify-the-hours-and-minutes-and-seconds-dropdowns would work just as well.

Note also that the function figures out itself whether the box is checked, making it idempotent: it's safe to call repeatedly during the process of checking a box.

Now we just need to bind that function to a few different events:

$("#all-day-checkbox").click(onCheckChange).change(onCheckChange);
$("label[for=all-day-checkbox]").click(onCheckChange);
VoteyDisciple
would i put this all inside (document).ready ??
ooo
@oo - you will need to put at least the bind code into the (document).ready, you can put the onCheckChange function into it, but that's not necessary.
karim79
what is the label second line at the bottom for?
ooo
select works fine but when i change it to select.hours or select.time it stops working. any suggestions?
ooo
The label click is important in the world of IE so your event will trigger if the user clicks the label instead of the checkbox, since the click event is what will fire first. `select.hours` will work fine as long as that `select` box has `class="hours"` I'd suggest giving them all `class="time-picker"` (or something similar), and then you can use `select.time-picker` in jQuery.
VoteyDisciple