user3168178
user3168178

Reputation: 1

jquery if condition is not working

I tried like this, after 2nd iteration if condition is failing

$('#next_page').click(function(){
    t = $('#totalPages').val();
    c = $('#currentPage').val();
    if(! (c > t)){
        var pageNo = parseInt($('#currentPage').val())+1;
        var sector = $('#sectorValue').val();
        var state = $('#states').val();

Upvotes: 0

Views: 61

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388316

Might be a problem with string comparison try

$('#next_page').click(function () {
    var t = +$('#totalPages').val();//or parseInt($('#totalPages').val())
    var c = +$('#currentPage').val();//or parseInt($('#currentPage').val())
    if (c <= t) {
        var pageNo = c + 1;
        var sector = $('#sectorValue').val();
        var state = $('#states').val();

Upvotes: 0

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

.val() always return a string value, which on arithmetic comparison behaves not in the way expected. So always before arithmetic operations convert these values, using:

parseInt(stringValue);

or simply putting + operator with string value which convert this string value to integer:

var t = +$('#totalPages').val();

you may also change your condition, simpler and readable:

if(c <= t){

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Try this,

var t = parseInt($('#totalPages').val());
var c = parseInt($('#currentPage').val());
if(! (c > t)){

Upvotes: 1

Related Questions