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 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    counting number of clicks

    I am trying to put in a counter that every time someone clicks a checkbox it will count up by one. If someone clicks on the checkbox again i still only want it counted once. If someone clicks but not on the check box i don't want it to count. If i can not do this with checkboxes does anyone else know any other way to do it. Once it counts to one if the person clicks on another checkbox i want it to go to 2.

    <script>
    var totalChecked=0
    function countCheckboxes(clickedBox){
    if(clickedBox.checked)
    totalChecked=totalChecked+1

    else
    totalChecked=totalChecked-1

    }
    </script>
    <form>
    <input type="checkbox" id="box1" onClick="countCheckboxes(this)">
    <input type="checkbox" id="box2" onClick="countCheckboxes(this)">
    </form>
    <script>
    document.write("score "+totalChecked)
    </script>

    This is what i tried and it looks like it would work but when i check a check box the score does not change from 0 to 1.
    Last edited by cwl157; 07-15-2003 at 10:13 PM.

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    var totalChecked=0
    function countCheckboxes(clickedBox){
    if(clickedBox.checked){
    totalChecked=totalChecked+1
    }
    else{
    totalChecked=totalChecked-1
    }
    }


    <input type="checkbox" id="box1" onClick="countCheckboxes(this)">


    Something like that should work? Obviously if you've got boxes that are checked by default on page load, your var totalChecked needs to be initialised to reflect this.
    Last edited by Spudhead; 07-15-2003 at 04:30 PM.


  •  

    Posting Permissions

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