View Full Version : stuck. ticking a checkbox changes the value of a field

09-16-2005, 12:42 PM
hi , afraid my javascript is quite basic (when i was at uni many years ago it appears i didn't pay enough attention :( )

i'm trying to do something very simple but getting no where fast ...

basically if a check box is ticked i want it to change the value to 0.5 and if it is unticked to run a function ... so far my code looks like this

<script language="javascript">

Function checkBox() {

if (document.form.halfDay.checked) {



which is initiated by the following in the form

<input type =checkbox name = halfDay onChange=checkBox>

can anyone help me please :)

09-16-2005, 12:59 PM
The first thing I noticed is that you have "function" capitalized. Javascript is very fussy about caps! In this case, the keyword 'function' should be all in lowercase.

Also when you call the onChange function, I believe you need ()'s: onchange="checkBox()". And, although they're not necessary, convention uses ""s around html values ( like I had around checkBox() ). Although in you code now it's fine, down the road you may have values that have multiple parts (eg onChange="alert('Check Box is Marked!'); checkBox();") -- without quotes, the space between the two parts would prevent the second part from happening (html wouls think this is a new html tag, not know what it means, and so would just ignore it)

09-16-2005, 01:01 PM
your very right , i'm no longer getting an error message at the bottom of my webpage but am not getting any value entered into the required field :confused:

09-16-2005, 01:06 PM
I just editted previous post. Re-read second part

09-16-2005, 01:10 PM
ah yes , i remember that now from my learning days , is always best to get into the habbit of following conventions, been such a long time since i've done any javascripting ( a lot easier when your at uni and have lecturers / class mates to help) rather than out in the world of work .....

thanks for your continued help ...

09-16-2005, 01:50 PM
:( still stuck ..... tried loads of other things and still getting no where

09-16-2005, 02:03 PM
oh man , you know what the problem was ....

there should have been only one = sign ... lol

that was one thing i thought i had remembered you always use 2 as in ==

thanks for your help was much appreciated .... ;)

09-16-2005, 02:04 PM
I don't believe a checkbox has an onChange event handler. Try onclick, but what are you trying to do? When the checkbox is clicked to add .5 and then when it's unchecked ?

09-16-2005, 02:08 PM
it seems my celebration was a little premature ... basically i want if the checkbox is ticked the field

lenghtOfAbsence = 0.5

but if it is no checked (or unchecked) then the function countDays is run

Canuck WebGeek
09-16-2005, 02:08 PM
This line:

is actually checking if the value of that field is equal to 0.5, not assigning a value to it.

remove one of the equal signs. Also, the parentheses(sp?) around document.form.lenghtOfAbsence.value are not needed. They don't hurt anything, but it's extra code that you can do without.

So it should be like this:
document.form.lenghtOfAbsence.value = "0.5";

(BTW, this is a mistake I do ALL the fricking time, and I've been writing Javascript since 1997 so don't feel too bad about it.)

But you just figured that out. N/M

If you want something to fire when it's not checked do this:

if (form.chckBox.checked) {
else {

Also, all input elements have an onchange event. With a checkBox, it fires just after the the onclick event. Onclick fires prior to the changing the checked value (so you can stop the change if needed).

09-16-2005, 02:14 PM
thanks .. just seems like such as a classic school boy error ..

I'm by no means a programmer (as is obvious) but can write much more effectively in SQL ...

my new problem now is that if the tick box is unchecked how do i get it to run the function countDays

i call countDays(this.form) earlier on in my code so if someone changes their mind and unchecks the check box i want it to revisit that function would i simply state



09-16-2005, 02:17 PM
bingo was his name o

cheers for your help and support guys , works a treat ....

i may have to stop back here in a few days (my next major thing to add is that when the form is submitted the record also gets emailed to certain people in the firm) .... i'll have a go at this myself first but if / when i get stuck i know where to come for some help

take care

Andy (very grateful)

Canuck WebGeek
09-16-2005, 02:21 PM
simply state




edited my earlier response to mention that.