views:

849

answers:

2

Hi folks Trying to create a simple spinner effect with Jquery, i.e. two buttons (up & down) with a text field. The upbutton increases the value while the down button decreses the value. increment steps + or - 1.

Any suggestions as ui.spinner is most def. not working and I am new to jquery. musty be something like $(#up).click (function ( /*SOMETHING GOES IN HERE but what?*/ )) and likewise for #down. both to set adjust the input text field say id #test as above.

A: 

Demo: http://jsbin.com/akiki

<button id="inc">+</button>
<button id="dec">-</button>
<input type="text" name="qty" value="0" />

<script type="text/javascript">
  $(function(){
    $("#inc").click(function(){
      $(":text[name='qty']").val( Number($(":text[name='qty']").val()) + 1 );
    });
    $("#dec").click(function(){
      $(":text[name='qty']").val( Number($(":text[name='qty']").val()) - 1 );
    });
  });
</script>
Jonathan Sampson
A: 

Maybe something like:

$(document).ready( function() {
  var el = $('#test');
  function change( amt ) {
    el.val( parseInt( el.val(), 10 ) + amt );
  }

  $('#up').click( function() {
    change( 1 );
  } );
  $('#down').click( function() {
    change( -1 );
  } );
} );
thenduks
Thanks thenduks works a treat, but ummm... being stupid now forgot to add something, how to go up/down a <ul><li> list in the same way - sorry
russell
@russel: Not sure what you mean... "go" up/down a list?
thenduks
AFAICS, this won't work if you want more than one on a page.
rjmunro
It would be trivial to make this work with multiple on a page. Start by changing the `#up` and `#down` id's to `.up` and `.down`, instead of just `change(1)` and using `el` in the function, you can find and then pass the target element along too... Easy.
thenduks