views:

112

answers:

4

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

A: 

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

ivans
but when it compare two numbers, what is the problem?
Syom
Here is the problem: http://stackoverflow.com/questions/61088/hidden-features-of-javascript/61545#61545
Michael La Voie
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
+3  A: 

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
+3  A: 

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
A: 

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
@Philippe Leybaert ok, in this case,why the following is false? if(14 > 12)..., even if it treats numbers as string?
Syom
@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
`(14 > 12)` will return true. `("14" > "12")` will also return true, but for different reasons. OTOH, `("15" > "124")` will also return true
Philippe Leybaert
@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
@Andy E's head - Ahh. I had suspected something like that, but was too lazy to test it. Thanks :)
Matchu