3

i have the following script

<select id="select1">
      <option value="1">1day</option>
      <option value="2">2day</option>
      <option value="3">3day</option>
    </select>

    <select id="select2">
      <option value="1">1day</option>
      <option value="2">2day</option>
      <option value="3">3day</option>
    </select>

and jquery

$("#select2").change(function() {
            var max_value = parseInt($("#select2 :selected").val());
            var min_value = parseInt($("#select1 :selected").val());
            if(max_value < min_value)
            {
              $("#select1").val($(this).val());
            }
        });

and now, what i can't understand anyway - if values of option elements are integer numbers, why i have to use parseInt()? in some cases it doesn't work without parseInt().

Thanks

Simon
  • 22,637
  • 36
  • 92
  • 121
  • 1
    Incidentally, you don't have to use `parseInt` here. [`+` will do as good a job](http://stackoverflow.com/questions/61088/hidden-features-of-javascript/2243631#2243631), e.g. `var max_value = +$("#select2 :selected").val();`. – Andy E May 29 '10 at 18:51
  • @Andy E's head - ...O_O. I never knew that trick! I can't decide if that looks prettier or uglier than an explicit conversion... but I'll probably go for it when I deal with a variable name or an object property rather than a method call. – Matchu May 29 '10 at 18:54
  • @Matchu: it's one of my favourite things about JS, saving all those bytes ;-) – Andy E May 30 '10 at 10:12

4 Answers4

5

Form field values are always stored as strings. Whether or not they look like integers is irrelevant; they're strings. You need to convert them to integers before treating them as such :)

Matchu
  • 83,922
  • 18
  • 153
  • 160
4

http://www.uvsc.edu/disted/decourses/mct/2760/IN/krutscjo/lessons/06/ff_05.html

Javascript treats most everything as a string unless you explicitly tell it that it is a number. One notable example of this is getting values from form elements. Depending on the browser and user input you may get some unexpected results.

Corey
  • 1,177
  • 4
  • 16
  • 24
1

Values are never integers as such, the fact that you put numbers there instead of who-knows-what is your choice only.

ivans
  • 1,465
  • 10
  • 13
  • but when it compare two numbers, what is the problem? – Simon May 29 '10 at 18:39
  • Here is the problem: http://stackoverflow.com/questions/61088/hidden-features-of-javascript/61545#61545 – Michael La Voie May 29 '10 at 18:41
  • 1
    If you really want to know, watching "JavaScript: The Good Parts" will help you get the problem with not converting to the appropriate type before comparing as well as just about every common mistake there is: http://www.youtube.com/watch?v=hQVTIJBZook – Michael La Voie May 29 '10 at 18:42
1

jQuery's val() function always returns a string. In many cases you can mix numbers and strings (in arithmic for example), when comparing two string variables, javascript will perform a string comparison, not a numeric comparison (which is to be expected)

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • ok, in this case,why the following is false? if(14 > 12)..., even if it treats numbers as string? – Simon May 29 '10 at 18:43
  • @Syom: are you sure that the form values are what you think they are? My Google Chrome Javascript console says that `"14" > "12"` is true. – Matchu May 29 '10 at 18:55
  • `(14 > 12)` will return true. `("14" > "12")` will also return true, but for different reasons. OTOH, `("15" > "124")` will also return true – Philippe Leybaert May 29 '10 at 18:58
  • @Matchu: When you assert `String() > String()`, the assertion is alphanumeric order based, so `"a" < "b" == true`, `"11" < "12" == true`, but `"19" > "131" == true` (9 is further down the character list than 3). This type of assertion with strings is commonly found in `array.sort()` functions. – Andy E May 30 '10 at 11:54
  • @Andy E's head - Ahh. I had suspected something like that, but was too lazy to test it. Thanks :) – Matchu May 30 '10 at 13:37
  • Then why do they write in the `.val()`'s docs that it "Returns: String or Number or Array"? – Tsahi Asher Jul 02 '13 at 06:46