Home > Net >  if statement does not work - script doesn't stop when skillpoints are below 0
if statement does not work - script doesn't stop when skillpoints are below 0

Time:02-07

I have if statement that doesn't work.

I want the script to stop working when the skillpoints are 0 or below.

My code:

var skillpoints = 5;

        if (skillpoints > 0) {

            // Dodawanie skillpointów
        
            $('.str .stat-plus img').click(function() {
                var val = $('.str-img div').text();
                var new_val = parseInt($('.str-img div').text(),10)   1 ;
                $('.str-img div').text(new_val);
                $('.str input').val(new_val);
                skillpoints--;
                $('.skillpoints').text(skillpoints);
                $('.register-skillpoints').val(skillpoints);
            });
    }

CodePudding user response:

You have to put the if condition into your click handler, like this:

$('.str .stat-plus img').click(function() {
  if (skillpoints > 0) {
    // etc
  }
}

Otherwise, the if condition is checked only once at page load, and never again when clicking.

  •  Tags:  
  • Related