Russell
Russell

Reputation: 1804

JQuery checking number and put lower than others

Here is my demo code. I want to make sure that num never cross num2. The code is functioning. but a simple problem. If num2 has 444 i cant put first value more than 4

$('#num').keyup(function() {
    var num = $('#num').val();
    var chk_num = $('#num2').val();
    if (num>chk_num) {
        $('#num').val(chk_num);
    }
});

demo code in jsFiddle: https://jsfiddle.net/n7bbjfpw/

Upvotes: 0

Views: 18

Answers (1)

dloeda
dloeda

Reputation: 1548

I think you are comparing strings. If you want to compare both values as integers you must parse them first with the function parseInt.

Upvotes: 1

Related Questions