views:

814

answers:

2

How do you compare a value from jQuery with a fixed number?

I thought this might work but it doesn't:

if (parseInt($("#days").value) > 7) {
            alert("more than one week");
        }
+4  A: 

if #days is an input then you need .val() instead of value

e.g.

  if (parseInt($("#days").val()) > 7) {
        alert("more than one week");
   }
redsquare
Oh for the love of $(#deity). Thanks! I can eat lunch now.
IainMH
+8  A: 

As well as @redsquare 's answer to use .val(), you should specify the radix:

if (parseInt($("#days").val(), 10) > 7) {
            alert("more than one week");
        }

This is because the value could have a leading 0, in which case parseInt would interpret the value as octal.

Greg
Nice +1, learn something new from SO everyday.
Jon Erickson