www.webdeveloper.com
Results 1 to 3 of 3

Thread: Help with Javascript button count

  1. #1
    Join Date
    Feb 2014
    Posts
    2

    Help with Javascript button count

    Hello people

    I am trying to do a button count that is very much like this:
    http://jsfiddle.net/7rTXD/

    I'm looking it to be in JavaScript only and not JQuery. No html either. Just 100% JavaScript. The idea is to limit the amount of clicks so that it wont go over 6 nor under 0. I have 6 images connected to a button and for each click I want the image to move to the next one. When I reach the last image the button can continue to be clicked which pushes them further away. So I want it to be limited to 6 as the max and then disable that button when it reaches the maxed number of clicks.

    It would be click the right button once, and it leaves 5 more clicks on the right button and 1 click for the left. Click the right button 4 times and it leaves 2 clicks for the right and 3 for the left button.

    Can someone tell me how this is done?

    Thanks

  2. #2
    Join Date
    Feb 2014
    Posts
    2
    Never mind. As soon as I was going to quit this task for now, finally got it working and running. The code was close enough.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,354

    Lightbulb

    It not very beneficial for other forum members or noobies when you post a question,
    say you have solved it and then close the thread without any example or working code.

    I don't know what you came up with, but here is one possible solution for others to view.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    
    </head>
    <body>
    <button id="btnMinus"> - </button>
    <input type="text" id="btnCount" value="0" size="3">
    <button id="btnPlus"> + </button>
    
    <p><button onclick="alert(document.getElementById('btnCount').value)">Current Count</button>
    
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?290045-Help-with-Javascript-button-count
    
    var btnCounter = (function () {
      var counter = 0;
      var maxCount = 6;
    
      document.getElementById('btnMinus').onclick = function() {
        counter--;  if (counter < 0) { counter = 0; }
        document.getElementById('btnCount').value = counter;
      }
      document.getElementById('btnPlus').onclick = function() {
        counter++;  if (counter > maxCount) { counter = maxCount; }
        document.getElementById('btnCount').value = counter;
      }
    })();
    
    </script>
    
    </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
  •  
HTML5 Development Center



Recent Articles