How do I check if a checkbox is checked?

Hi everybody,
I want to be able to use javascript to check if a checkbox is selected. If my checkbox has the following html code

<input type="checkbox" name="cbox1" id=\\"cbox1" checked="yes">

However when I use this, I always get null, even when the box is checked.

var cbox1=document.getElementById(cbox1);
alert(cbox1);

Thanks in advance.

Thanks Logic Ali, that works :slight_smile:

document.getElementById expects a string parameter.
It’s not clear if this code is running in global scope, but names of global variables must not conflict with IDs.

var cb1 = document.getElementById( "cbox1" );
alert( cb1.checked );

Hi siteguru, I the \ character is from my php code, I just pasted it incorrectly.

I have tried this and it does not work.

var cbox1=document.getElementById(cbox1).checked;
alert(cbox1);

This is not valid?

Why the \ at id=\“cbox1”? Typo?

Anyway, it is the .checked status you want to test - it will be either true (1) or false (0). :slight_smile:

Try googling for ‘javascript checkbox’. :slight_smile: