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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Help, Click Counter

    Hey Everyone,

    I'm trying to set up two different buttons that count their own individual clicks.

    So far I have everything except where one button leaves off, the other picks up. So if I click the left one three times it shows the number 3, and then if I click the button next to it, that button picks up at 4. I want each button to have its own count.

    Any ideas?

    Here's what I have so far.


    <html>
    <head>
    <script type="text/javascript">
    var counter = 0;
    function increaseloveit() {
    counter += 1;
    document.getElementById('loveit').value = counter;
    }
    var counter = 0;
    function increasehateit() {
    counter += 1;
    document.getElementById('hateit').value = counter;
    }
    </script>
    </head>
    <body>

    <form>
    <a href="#" onclick="javascript:increaseloveit();"><input type="button" value="Love It!" name="loveit" onclick="" /></a><input id="loveit" value="0" size="2" /></form>
    <form>
    <a href="#" onclick="javascript:increasehateit();"><input type="button" value="Hate It!" name="hateit" onclick="" /></a><input id="hateit" value="0" size="2" /></form>

    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Well, as both variables are named counter you would expect that! Name them counterA and counterB or something.

    I expect you realise that the number of clicks recorded will apply only to that particular user in that browser session. If you are trying to create a poll where all users can see the results you will need server-side scripting.


    "Think about how stupid the average person is, and then realize that half of them are more stupid than that!" - George Carlin
    Last edited by Philip M; 07-27-2010 at 08:35 AM.

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahh. Thanks a lot!


  •  

    Tags for this Thread

    Posting Permissions

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