jQuery: issue with converting text to integer (input value)

user2571510

I am using the following code on a page from me where I cant figure out how the switch can recognise this as a number as it always fails.

My value is a standard integer that varies and the code works fine if I hard-code it but not when I just it to the variable. I tried parseInt as well. I always get the result for < 1 as it reads my number as text / 0. The input value is filled in with PHP.

I hope someone here can help me with this.

My input field:

<input type="hidden" class="span12" id="daysRest" name="daysRest" value="40" />

The code where I want to use it:

daysRest = $('#daysRest').val();

            switch(true) 
            {
                case(daysRest < -999):
                    bulletColor = 'white';
                    daysResttxt = '';
                    break;
                case(daysRest < 0):
                    bulletColor = 'red';
                    daysResttxt = daysRest + ' days remaining';
                    break;
                case(daysRest < 1):
                    bulletColor = 'red';
                    daysResttxt = 'less than 1 day remaining';
                    break;
                case(daysRest < 4):
                    bulletColor = 'yellow';
                    daysResttxt = daysRest + ' BD remaining';
                    break;
                default:
                    bulletColor = 'white';
                    daysResttxt = '';
                    break;
            }

            $('#daysRest').val(daysRest);
            $('#spanRest').text(daysResttxt);   

Many thanks for any help with this, Tim.

user2571510

Just needed a foreach loop in PHP and so i moved all the above there - works fine now. Thanks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related