Be aware of the difference between using onclick and onchange on a checkboxes (and radio buttons). OnClick happens BEFORE the change occurs, so if you click a checkbox that is currently checked, onclick will see the box as checked and onchange will see it as unchecked.
Be aware of the difference between using onclick and onchange on a checkboxes (and radio buttons). OnClick happens BEFORE the change occurs, so if you click a checkbox that is currently checked, onclick will see the box as checked and onchange will see it as unchecked.
Hmmm, I could've sworn that worked.... but you're right.
Very interesting indeed. If you return false from the onclick event, the checkbox will change on the event firing and then change back when you return false. I have a feeling that's not supposed to happen, I'll do some digging.
ussually almost all the events will fire first the javascript function than their default HTML action - if any, for instance onsubmit. For unknown reasons (or for good reasons) onclick (and onkeyup, and some other few, I guess) do the opposite.
No, that's no entirely true. onclick fires before the default action for hrefs, which is why you can cancel the link before you follow it.
That leads me to believe that there's something weird going on with the way checkboxes handle onclick, but it's definitely not onclick across the board.
I created 4 radio buttons, none default selected, each with onclick="alert(this.checked);return false;"
If you none are selected when you click one it alerts true and selects it.
But then, if you click any other one after that, you get an alert that says true, and visually, the radio button selection has changed, but when click "OK" on the alert, the radio button snaps back to the first one you selected.
don't, is the same as checkboxes, I know that for long while. I use onclick and this.checked for long time ago to validade or calculate values for checkboxes and radios.
I understand that this has been the behavior for some time, but doesn't it seem unusual that instead of cancelling the default behavior, the default behavior actually happens and then you reverse it? What could be the explanation for this? Why would you cancel the default action before it happens on links and on buttons, but not checkboxes or radio buttons?
And why is it that for radio buttons, if none is selected, and you return false onclick (which should cancel the default behavior) it still selects one the first time but not subsequent times?
I'm going to have dig through the w3c dom standard on this to see what they have to say about some of it, but I'm currently of the opinion that this is not standards compliant behavior.
I don't think is non-standard. As a coder I feel that for checkboxes and radios things must be exactly like they are. I do need to have the HTML checked status triggered before I do something with my function.
But that's what onchange would be for. If you want to fire an event when the checkbox value changes, that's one thing. If you want to prevent the checkbox from changing values when someone clicks on it (based on some dynamic conditions) then you have onclick for that. If you return false on the onclick, it even squelches the "change" event from firing, but somehow the value of the checkbox is changed ONLY for the duration of the click event. It just doesn't make sense.
I don't think it's necessary or even sensible for the onclick behavior to do what it's doing when you have onchange to satisfy the need you have.