Robert
Robert

Reputation: 1159

Monitoring the check state of a checkbox

What is the best way to automatically check if a checkbox has been (un)checked. I know that the onclick event will catch click events but what if another script changes the state of the checkbox ala "check all button".

Upvotes: 2

Views: 1177

Answers (5)

KooiInc
KooiInc

Reputation: 122916

You can't use the change event to monitor for programmatic changes (changes, not originated from a click) to the checkbox. So you'll have to make up your own monitor. One way is to use setInterval for periodic checking. Something like: setInterval(checkstate,2000);. See this this jsfiddle to see an example in action.

Upvotes: 1

Aleadam
Aleadam

Reputation: 40391

using the checked property and the CheckboxStateChange event. I don't know about compatibility, though.

https://developer.mozilla.org/en/XUL/checkbox

https://developer.mozilla.org/en/XUL/Events

http://www.javascriptkit.com/javatutors/radiocheck.shtml

Upvotes: 1

deostroll
deostroll

Reputation: 11975

The other scripts should initiate a checkbox click in code itself. The event will bubble even if initiated from script. Here is an example if you are interested --> http://jsfiddle.net/qSRzM/1/

Upvotes: 0

Wasim Karani
Wasim Karani

Reputation: 8886

You can define one global variable in java script and change its state to checked
or unchecked on click that changes the state of check box and on another script changes
the state of the check box

Upvotes: 0

tilleryj
tilleryj

Reputation: 14379

You can use the 'change' event. The behavior is buggy in IE, so you might have to observe both click and change - http://www.quirksmode.org/dom/events/change.html.

Upvotes: 2

Related Questions