CodeIgniter Forums
Checkbox and Jquery - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Checkbox and Jquery (/thread-32845.html)

Checkbox and Jquery - El Forum - 08-06-2010

Hi I'm having a problem getting the value of the checkbox into my controller.
The problem is the alert windows is always telling me the value is unchecked(0).
Can someone please tell me what i'm doing wrong?

function getSubs(){
var root = "<?=base_url() ?>";
if ($("#subscribe").is("checked")) {  
    // checkbox is checked
    var checked=1;
    $().load(root+"user/subscribe", {subscribe: checked} );
    } else {
        // checkbox is not checked
        var unchecked=0;
        $().load(root+"user/subscribe", {subscribe: unchecked} );
       return false;


    if ($subscribe=='y'){
        } else {
    <?php echo form_open('user/subscribe'); ?>        
    <?php $js = 'id="subscribe"';?>
           $(document).ready(function() {
           $("#subscribe").bind("change", getSubs);
        <td >&lt;?php echo form_label('Subscribe : '); ?&gt;</td>
        <td id="subscribe">&lt;?php echo form_checkbox('subscribe',$subscribe,$js); ?&gt;</td>
&lt;?php echo  form_close(); ?&gt;

Checkbox and Jquery - El Forum - 08-06-2010


Also, you've assigned the ID to the td, so this won't work anyway. You can't have two elements with the same ID and expect javascript to know which one you mean. You need to change the selector to look for the checkbox inside #subscribe, or give the ID to (only) the checkbox itself.

Checkbox and Jquery - El Forum - 08-06-2010

Thanks. It's working now.