rusly
rusly

Reputation: 1524

else if in javascript

I'm trying to calculate a string based on user input and show some message:

$('#myInput').keyup(function() {
    if(this.value.length<=158)
     $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 || this.value.length<316)
         $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316 || this.value.length<400)
        $('#charCount').text('We will deduct 3 credit from your account');
});

http://jsfiddle.net/p9jVB/11/

The problem is the last else if is not working... Have I missed something?

Upvotes: 0

Views: 6990

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

else if(this.value.length>158 || this.value.length<316)
    $('#charCount').text('We will deduct 2 credit from your account');
else if(this.value.length>316 || this.value.length<400)
    $('#charCount').text('We will deduct 3 credit from your account');

Every number in the world is greater than 158 or less than 316, so the alternative condition will never be reached.

Upvotes: 4

Osiris
Osiris

Reputation: 4185

It looks like you mean &&, not ||

$('#myInput').keyup(function() {
    if(this.value.length<=158)
         $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 && this.value.length<=316)
         $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316 && this.value.length<400)
        $('#charCount').text('We will deduct 3 credit from your account');
});

Upvotes: 7

Related Questions