tags:

views:

456

answers:

7

I want to have a client side Plus / Minus system where a user can click the plus and a value is incremented by 1, minus and the value is decreased by 1, the value should never go below zero and should start on 0. Is there a way to do this simply in jquery? All the jquery plugins look at bit OTT for this operation.

Any help appreciated, ta.

+1  A: 
var currentValue = 0;

$(document).ready(function() {
  $('#up').click(function() {
    currentValue++;
  });
  $('#down').click(function() {
    if (currentValue > 0) {
      currentValue--;
    }
  });
});

Putting currentValue in a textfield or other element would then be trivial.

Aistina
+2  A: 

something like this:

HTML:

<a id="minus" href="#">-</a>
<span id="value">0</span>
<a id="plus" href="#">+</a>

Javascript:

$(function(){

    var valueElement = $('#value');
    function incrementValue(e){
        valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment, 0));
        return false;
        }

    $('#plus').bind('click', {increment: 1}, incrementValue);

    $('#minus').bind('click', {increment: -1}, incrementValue);

    });

Edit: Optimized code

Rowan
for some reason when this is using add it's incrementing it like 0, 01, 011, 0111 etc.
1''''
just realised it needs to be parsed as an int! var value = parseInt($('#value').text(value));
1''''
doh! my bad, I'll amend
Rowan
Wow, this could *really* be refactored!
Josh Stodola
@Josh refactored as in "it's good", or refactored as in "it's bad"?
Rowan
Refactored as in... I see a lot of repeating code.
Josh Stodola
Better now? I considered your comment a challenge. If there really is a much better way of doing it, I'd be interested to know: self improvement is objective #1 in our field!
Rowan
A: 
<span id="value">5</span>
<input type="button" id="plus" value="+" />
<input type="button" id="minus" value="-" />

$('#plus').click(function() { changeValue(1); });
$('#minus').click(function() { changeValue(-1); });

function changeValue(val) {
    var container = $('#value');
    var current = parseInt(container.html(), 10);

    container.html(Math.max(0, current + val).toString());
}
David Hedlund
Your `#span` selector is wrong .. it should be `#value`
Gaby
right you are. edited.
David Hedlund
+1  A: 

Sure you can, e.g.:

<span id="minus>-</span>
<span id="value">0</span>
<span id="plus">+</span>

<script type="text/javascript">  
   $(function() {
      var value = parseInt($('#value').text(value));

      $('#minus').click(function() {
         if (value == 0) return;
         value--;
         $('#value').text(value);
      }

      $('#plus').click(function() {
         value++;
         $('#value').text(value);
      }
  });
</script>
Felix Kling
A: 

The jquery part

// initialize counter
var counter = 0;

// set the + functionality
$('#plus').click( function(){$('#display').html( ++counter )} );
// set the - functionality
$('#minus').click( function(){$('#display').html( (counter-1<0)?counter:--counter )} );
// initialize display
$('#display').html( counter );

and the html part

<span id="plus">+</span>
<span id="minus">-</span>
<div id="display"></div>
Gaby
A: 

JQuery SpinButton Control

mangokun
A: 

This is actually built into jQuery - http://docs.jquery.com/UI/Spinner

Andrew