www.webdeveloper.com
Results 1 to 7 of 7

Thread: Simple Coding Project: Button with Global Counter

  1. #1
    Join Date
    Dec 2010
    Posts
    5

    Question Simple Coding Project: Button with Global Counter

    Hey there

    I'm learning the basics of HTML, CSS, and Javascript and I have an easy project to anyone who would oblige.

    I need a button (nothing fancy, just a regular button) that displays the global number of times it has been clicked. Sadly, these concepts are still beyond me at this point and though I can make a button, and make a counter, I need to link the two. Creds to whoever helps out.


    Thanks

    Rockethunder

  2. #2
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,477
    Hi there rockethunder,

    does this help...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>my little button</title>
    
    <style type="text/css">
    #mybutton {
        display:block;
        width:4em;
        margin:auto;
        cursor:pointer;
     }
    </style>
    
    <script type="text/javascript">
    function init(){
       var c=1;
    document.getElementById('mybutton').onclick=function() {
       this.value=c++;
      }
     }
     window.addEventListener?
     window.addEventListener('load',init,false):
     window.attachEvent('onload',init);
    </script>
    
    </head>
    <body>
    
    <div>
     <input id="mybutton" type="button" value="0">
    </div>
    
    </body>
    </html>
    
    coothead

  3. #3
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,114
    Funny.
    I started a project to make a button generator once and thought of putting up this script too, but this project has been left half done... got bored with it.

    If you like, you can use the button generator script.
    button generator

  4. #4
    Join Date
    Dec 2010
    Posts
    5
    Thanks coothead!

    The button works great but I'm looking to add a tad more funtionality:

    Could the button somehow recall how many times it's been pressed?

    For instance, I press the button ten times and come back later to find that someone else has pressed the button nine times and instead of the button reading "0" it reads "19".


    Thanks!

  5. #5
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,477
    Hi there rockethunder,

    to "add a tad more funtionalit" would require server-side php or similar.

    Does your server provide this facility?

    coothead

  6. #6
    Join Date
    Dec 2010
    Posts
    5

    Why yes indeed...

    My server is hosted from under my desk at home. I have SSH, SFTP, and physical access to it. I'm pretty sure I have LAMP installed as well. It's a relatively low-traffic site if that matters at all...

    Thanks
    Ezra

  7. #7
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,477
    Hi there rockethunder,

    check out the attachment, for a PHP version of your button.

    coothead
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles