PDA

View Full Version : JS events with checkbox and label


agent002
02-28-2004, 06:24 AM
I have been struggling with this problem and I finally came here to ask about it.

I have a checkbox within a label. The checkbox calls a JavaScript function whenever it's checked/unchecked, using the onClick event. It works fine, but when you click somewhere in the label area, the JavaScript isn't called. I tried using onChange instead of onClick, with the same result. Then I tried to add onClick to the label, but with a new problem - the JavaScript is called before the checkbox value is changed.

To see what I mean, try the attachment in my post. All help is appreciated.

agent002
02-28-2004, 06:34 AM
oh well. I solved the problem. Somehow my creativity launches as soon as I have posted here :P

But for future reference, this is what I did. First, I moved the checkbox outside the label, then made the label imitate a click of the checkbox using document.getElementById('cb').click(), and added return false to it to prevent the label from the actual action (which would check/uncheck the checkbox a second time). That acts just like a normal label, but also calls the JavaScript for the checkbox's onclick event.

See the attachment for the code.

Willy Duitt
02-28-2004, 09:08 AM
Talking to yourself now? :P
Really; I'm glad to see you worked it out. :D

.....Willy

agent002
02-29-2004, 07:43 AM
at least we have it here for reference...