AlwaysStudent
AlwaysStudent

Reputation: 1374

jquery keyup removeClass doesn't work

I have one question.

I am trying to make a addClass, removeClass button function for after the text length > 5 or something else.

So the addClass working fine in my code but removeClass doesn't work. What I am missing here or what I am doing wrong here anyone can help me in this regard ?

I have tried the following code also please click for DEMO:

// Keypress for active inactive button
    $(".PGA51c").on('keyup', function() {
      var ID = $(this).attr('data-id');
      var cm = $('#PCm' + ID);
      if (cm.text.length < 5) {
         $('#sev' + ID).addClass('tCo');
         console.log('OK');
      } else {
         $('#sev' + ID).removeClass('tCo');
         console.log('Not OK');
      }
   });
});

Upvotes: 0

Views: 161

Answers (1)

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

Use text() instead of text in if condition.

$(".PGA51c").on('keyup', function() {
    var ID = $(this).attr('data-id');        
    var cm = $('#PCm' + ID);
    if (cm.text().length < 5) {
    //-----^^^^^^--------------
        $('#sev' + ID).addClass('tCo');
        console.log('OK');
    } else {
        $('#sev' + ID).removeClass('tCo');
        console.log('Not OK');
    }
});

Upvotes: 2

Related Questions