Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    stuck. ticking a checkbox changes the value of a field

    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

    Code:
    <script language="javascript">
    
    Function checkBox() {
    
    if (document.form.halfDay.checked) {
    	(document.form.lenghtOfAbsence.value)=="0.5";
    }
    
    }
    
    </script>
    which is initiated by the following in the form

    Code:
    <input type =checkbox name = halfDay onChange=checkBox>
    can anyone help me please

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)
    Last edited by Pyth007; 09-16-2005 at 01:05 PM.

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just editted previous post. Re-read second part

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ...

  • #6
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still stuck ..... tried loads of other things and still getting no where

  • #7
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ....

  • #8
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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 ?

  • #9
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    New Coder
    Join Date
    Sep 2005
    Location
    Ottawa, ON
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This line:
    (document.form.lenghtOfAbsence.value)=="0.5";

    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) {
    dothis();
    }
    else {
    dothat();
    }

    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).
    Last edited by Canuck WebGeek; 09-16-2005 at 02:19 PM.

  • #11
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    else
    countdays(this.form)

    ?

  • #12
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #13
    New Coder
    Join Date
    Sep 2005
    Location
    Ottawa, ON
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    simply state

    else
    countdays(this.form)

    ?
    Yep.

    edited my earlier response to mention that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •