pepe
pepe

Reputation: 9909

jQuery to disable dropdown on checkbox click AND insert TRUE/FALSE value

I am using this code to disable a dropdown when user clicks on a checkbox

function onCheckChange() {
  if ($("#all_day_checkbox").is(':checked'))
    $("#evt_time_drop").attr('disabled', 'disabled');
  else
    $("#evt_time_drop").removeAttr('disabled');
}

$("#all_day_checkbox").click(onCheckChange).change(onCheckChange);

This works fine. But I would like to add an instruction to inject a true or false value to the checkbox so this can be stored in the database.

Unfortunately this doesn't work:

function onCheckChange() {
  if ($("#all_day_checkbox").is(':checked'))
    $("#evt_time_drop").attr('disabled', 'disabled');
    $("#all_day_checkbox").val("TRUE");
  else
    $("#evt_time_drop").removeAttr('disabled');
    $("#all_day_checkbox").val("FALSE");
}

$("#all_day_checkbox").click(onCheckChange).change(onCheckChange);

Anyone have suggestions to get to work?

Thanks for your help!

Upvotes: 1

Views: 5520

Answers (3)

Irving
Irving

Reputation: 1

function onCheckChange() {  
if ($("#all_day_checkbox").is(':checked')){
    $("#evt_time_drop").attr('disabled', 'disabled');    
    $("#all_day_checkbox").val("TRUE");   
}
else  
{  
    $("#evt_time_drop").removeAttr('disabled');  
    $("#all_day_checkbox").val("FALSE"); 
} 
}

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

Try this snippet:

$("#all_day_checkbox").val("FALSE");//For initially make the `checkbox` value FALSE

function onCheckChange() {
    if ($("#all_day_checkbox").is(':checked')) {
        $("#evt_time_drop").attr('disabled', 'disabled');
        $("#all_day_checkbox").val("TRUE");
    } else {
        $("#evt_time_drop").removeAttr('disabled');
    }
}
$("#all_day_checkbox").click(onCheckChange).change(onCheckChange);

Upvotes: 2

Thomas Shields
Thomas Shields

Reputation: 8942

What about

$("#all_day_checkbox").attr("checked", "") //for removing the check
$("#all_day_checkbox").attr("checked","checked") //for checking the box

And then just checking to see if the "checked" value equals "checked"...

Upvotes: 2

Related Questions