Results 1 to 3 of 3

Thread: count up

  1. #1

    count up

    i am try to use javascript to count up like 1, 2, 3 .. but only when a button is clicked. so each click add 1 so if the buttons been clicked 10 it should disply the number 10.
    (it is more like a Download/Click Counter)
    how would i do this???
    Last edited by james182; March 28th, 2006 at 05:09 AM.

  2. #2
    lol I don't know javascript but just use this pseudo:

    name.textfield=0

    if counter.Button = push
    name.textfield= name.textfield+1

    lol

  3. #3
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
     <head>
      <title> </title>
    
      <script type="text/javascript">
    var counter = 0;
    
    function countUp(){
      counter++;
      document.getElementById('countUp').innerHTML = counter;
    }  </script>
    
     </head>
     <body>
    
      <div id="counter">
       <span id="countUp">0</span><br />
       <input type="button" name="cButton" value="Count Up" onclick="countUp()" />
      </div>
    
     </body>
    </html>
    It's pretty simple really. Make sure to declare the counter variable outside of your function. Then create a function that will increase the counter variable each time the button is clicked and display it. For the button you're just using onclick to call the function.

    I didn't know where you wanted the number displayed so I just tossed it into a span tag using getElementById(). You could just as easily use a form and text field. Like so:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
     <head>
      <title> </title>
    
      <script type="text/javascript">
    var counter = 0;
    
    function countUp(){
      counter++;
      document.counterForm.displayCounter.value = counter;
    }  </script>
    
     </head>
     <body>
    
      <form name="counterForm">
       <input type="text" name="displayCounter" value="" /> <br />
       <input type="button" name="cButton" value="Count Up" onclick="countUp()" />
      </form>
    
     </body>
    </html>
    Γνώθι Σεαυτόν

Thread Information

Users Browsing this Thread

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

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012