Categories:

JavaScript Kit > JavaScript Reference > Here

Checkbox Object

Last updated: June 20th, 2004

Checkboxes (<input type="checkbox">) within your form can be accessed and manipulated using JavaScript via the corresponding Checkbox object. There are two ways to access a checkbox, depending on whether it it unique in the form (distinct name), or part of a group of checkboxes, all sharing the same name.

Distinct Name:

document.myform.mycheck //where myform and mycheck are names of your form/element.
document.myform.elements[i] //where i is the position of the checkbox within form

Shared Names (part of a group of checkboxes):

document.myform.mycheck[i] //where i is the position of the checkbox within the group of checkboxes.

Related Tutorials

Events

Events Description
onBlur Code is executed when the focus is removed from the checkbox.
onClick Code is executed when user clicks on the checkbox.
onFocus Code is executed when the focus is set on the checkbox.

Properties

Properties Description
accessKey String value that sets/ returns the accessKey for the checkbox.
checked Boolean property that reflects the current state of the checkbox, "true" if it's checked, and "false" if not. Example(s)
defaultChecked Boolean property that reflects the value of the CHECKED attribute.
disabled Boolean value that sets/ returns whether the checkbox is disabled.
form References the form that contains the checkbox.
name Reflects the name of the checkbox (the name attribute).
type A property available on all form elements, "type" returns the type of the calling form element, in this case, "checkbox".
value A read/write string that specifies the value of the checkbox (the value attribute).

Methods

Methods Description
blur() Removes focus away from the checkbox.
click() Simulates a user clicking on the checkbox.
focus() Sets focus on the checkbox.

Examples

checked

This example loops through a group of checkboxes all sharing the same name, and alerts a message if one is found to be checked:

<form name="test">
<input type="checkbox" name="checkgroup" checked />
<input type="checkbox" name="checkgroup" />
<input type="checkbox" name="checkgroup" checked />
</form>

<script type="text/javascript">
for (i=0; i<document.test.checkgroup.length; i++){
if (document.test.checkgroup[i].checked==true)
alert("Checkbox at index "+i+" is checked!")
}
</script>


Reference List

Partners
Right column

CopyRight 1998-2014 JavaScript Kit. NO PART may be reproduced without author's permission.