SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Displaying how many times a button/div is clicked

    Hello,
    I have an idea for a script that I was hoping someone at the js forum could help me start or write a basic version of because i have no clue where to start it.

    So what I am trying to accomplish is counting how many times a div (if necessary it can be a <button> or <input />) has been clicked and then displaying it. The counting meaning numbers of times clicked so that would have to be stored in a variable and then if the user clicks that number or variable is incremented by one.

    Is this possible to do in js and if so can someone please help me start writing this ?

    I would really appreciate any and all help with this

    Thanks in Advance & Best Regards,
    Team 1504

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Have a look at the following jsFiddle which i created a simple demo in, basically it just creates a new element after the target element and increments the count with each click.

    http://jsfiddle.net/UXUYB/2/

  3. #3
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hmm cool! that was exactly what i was looking for. thank you very much!

    Do you know how one could limit the user to only clicking once? like a voting system?

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you only need the button to be clicked once then a counter isn't even needed, see the below jsFiddle which makes use of the element object to detect if it has been clicked.

    http://jsfiddle.net/chrisupjohn/kRVwf/

  5. #5
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hmm okay, but why get rid of the counter
    I was thinking of something that counts how many different people / computers voted or clicked the button. sorry if i was confusing before

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The counter is user specific because JavaScript is a browser based language which doesn't communicate with a server at any point in time unless your code tells it to, if you want a total count of how many people have clicked the button the vote count would be a clear indication of that wouldn't it?

  7. #7
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    The counter is user specific because JavaScript is a browser based language which doesn't communicate with a server at any point in time unless your code tells it to, if you want a total count of how many people have clicked the button the vote count would be a clear indication of that wouldn't it?
    no it wouldn't. But then people could alter the vote by clicking countlessly. so would PHP need to be involved then?

    do you know how one would check with the server to see if the user has voted and if they have then show the button and the total votes, but do not allow there votes to count; and, if they havent voted allow there one vote to count?

    could that be done in PHP or Perl?


Bookmarks

Posting Permissions

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