tora0515
tora0515

Reputation: 2547

Stop quantity spinner from going negative

I have a problem with a spinner. I want to stop the decrease function from working if the quantity is 0. Here is the code:

$(document).ready( function() {
      var elm = $('#htop');
              function spin( vl ) {
                elm.val( parseInt( elm.val(), 10 ) + vl );
              }

              $('#increase').click( function() { spin( 1 );  } );
              $('#decrease').click( function() { spin( -1 ); } );
    });

I am guessing I need to do something with the

$('#decrease').click( function() { spin( -1 ); } );

or maybe I should try wrapping an if statement around

function spin( vl ) {
                elm.val( parseInt( elm.val(), 10 ) + vl );
              }

Thanks in advance.

Upvotes: 0

Views: 673

Answers (1)

Brian Glaz
Brian Glaz

Reputation: 15676

stop the decrease function from working if the quantity is 0

Just turn this in to code and you'll be fine:

function spin( vl ) {
    if(elm.val() > 0) { //as long as the spinner is above 0
        elm.val( parseInt( elm.val(), 10 ) + vl );
    }
}

Upvotes: 2

Related Questions