1

I have a simple calculator with 2 tabs that i am building. One of those tabs are disabled with a class called .not-active. I want to remove that class when the value of #amount is > 195. However even though the if statement is in place it removes the class upon change of value, Even when value is < 195.

Here is my jquery function:

$(function() {
$( "#slider-range-min1" ).slider({
  range: "min",
  value: 40,
  min: 5,
  max: 700,
  step: 5,
  slide: function( event, ui ) {
    $( "#amount" ).val( ui.value );
    if ($('#amount').val() > "195") {
        $('#l2').removeClass('not-active');
    };

  }
});
$( "#amount" ).val( $( "#slider-range-min1" ).slider( "value" ));

});

5 Answers 5

1

Use parseInt and integers

$(function() {
$( "#slider-range-min1" ).slider({
  range: "min",
  value: 40,
  min: 5,
  max: 700,
  step: 5,
  slide: function( event, ui ) {
    $( "#amount" ).val( ui.value );
    if (parseInt($('#amount').val()) > 195) {
        $('#l2').removeClass('not-active');
    };

  }
});
$( "#amount" ).val( $( "#slider-range-min1" ).slider( "value" ));

});
Sign up to request clarification or add additional context in comments.

Comments

0

To compare properly, you need to ensure that both values are integers. You can use parseInt() to convert the #amount value. Try this:

if (parseInt($('#amount').val(), 10) > 195) {
    $('#l2').removeClass('not-active');
};

Comments

0

You cannot compare int with string. Change it to:

if (parseInt($('#amount').val()) > 195) {

You have to remove quotes and use parseInt().

Comments

0

Use parseInt(). Actually the value you get from the input field type is string so you need convert to integer. You couldn't check string type with > operator.

if (parseInt($('#amount').val(), 10) > 195) {
        $('#l2').removeClass('not-active');
    };

Comments

0

The problem, as already pointed out by others, is you are comparing the collation order of strings instead of the values of two numbers.

e.g.

"2" > "123" == true!

You need to convert the string to a number first and compare against another number.

If the numbers are not astronomical in range (i.e. not outside the range of an standard integer... which is pretty huge), you can use ~~ to convert a string to an integer. As well as shorter, it is also faster than parseInt:

e.g.

if (~~$('#amount').val() > 195){

1 Comment

Perhaps JqueryKing would care to explain why he down-voted this correct and useful alternative answer? If he is such an expert he should have known this trick already :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.