Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

if statement does not work – script doesn't stop when skillpoints are below 0

I have if statement that doesn’t work.

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

My code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
            });
    }

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading