Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery checkbox always returns 'undefined'

Tags:

Info: jQuery 1.6.0 HTML checkbox:

First I've made sure jQuery is loaded with:

if(jQuery) {
    alert("loaded");
}

And this is the case, it's loaded. Then immidiately the line after that I use:

var returnvalue = jQuery("#mycheckbox").attr("checked");
alert(returnvalue);

This, for some reason, always alerts 'undefined'. I'm 100% sure the ID exists. I've also tried the following method:

jQuery("#mycheckbox").find('input[type="checkbox"]').each(function(){
    alert("foo");
    if (jQuery(this).is(":checked")) 
        ReturnVal = true;
});

But this doesn't even pop up the alert.

What am I doing wrong? I'm by far a jQuery or Javascript expert, but this should work... right?

Edit: I've tried:

jQuery(document).ready(function() {
    alert(id);
    var returnvalue = jQuery("#" . id).prop("checked");
    alert(returnvalue);
});

The alert(id) gives me the proper ID, but alert(returnvalue) returns undefined.

I've also tried:

if(jQuery) { 
    alert("loaded");
} 
jQuery(function() {
    alert(id);
    var returnvalue = jQuery("#" . id).attr("checked");
    alert(returnvalue);
});

Same result

Edit2: After using .prop AND appending the id with a '+' instead of a '.' (PHP habit), it works. Thank you all for your help!