views:

42

answers:

3

HTML:

<select id="staylength" name="staylength">
  <option disabled="disabled" value="1">1</option>
  <option disabled="disabled" value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>

In Google Chrome (5.0.375.55 on WinXP), the following jquery code fails to set the value of staylength.

var my_min = 3;
$('#staylength').val(my_min);

But the same, when run from javascript console in Google Chrome, is successful. What Is wrong?

Edit
Here is the offending page: http://dv2.makestay.com/node/1

To reproduce the error select either '06/21/2010' or '06/22/2010' as Check-In Date. The value failing to get updated is in Nights select.

EDIT 2

This is the whole function. It is called with parameter set to 3:

function apply_restriction(i) {
    var my_index = parseInt(i);
    var my_option;
    var my_min = min_stay[my_index];
    $('#staylength').html('');
    for (var j=1; j < 15; j++) {
        if (parseInt(j) < parseInt(my_min)) {
            my_option = '<option value="' + j + '" disabled="disabled">' + j + '</option>';
        } else {
            my_option = '<option value="' + j + '">' + j + '</option>';
        }
        $('#staylength').append(my_option);
    }
    $('#staylength').val('' + my_min);
    alert($('#staylength').length);
}
+2  A: 

Updated

The issue is this line:

var my_min = min_stay[i] + 1;

min_stay is an array of strings, so this is resulting in "31", not 4 like you want (and "31" is outside the values in the list, resulting it it defaulting back to the first <option>), you either need to return an array of integers in your response, instead of this:

min_stay.push('3', '3');
//should be:
min_stay.push(3, 3);

or, use parseInt() like this:

var my_min = parseInt(min_stay[i], 10) + 1;
Nick Craver
As I explained in a comment above, that is not the cause.
Majid
@Majid - Try `alert($('#staylength').length)`, what do you get?
Nick Craver
@Majid - It's setting it to 3, 3, 1 then 31, let me see why in the logic...the `.val()` is working, but 31 isn't a valid value.
Nick Craver
@Nick Craver: `alert($('#staylength').length)` returns 1. Shouldn't it return 5 for the snippet, and 14 for the actual page?
Majid
@Majid no the "length" there is just the number of elements matched by the '#staylength' selector. I don't see the code you're talking about in that page. Where is the code that tries to set the value of the selector? Does this work in Firefox?
Pointy
@Majid - The issue is string/number typing, see my updated answer for why and a solution :)
Nick Craver
@Nick Craver: Yes it works in Firefox, but with firefox there is no need for it since the first non-disabled option is automatically selected. And for the code, I pull it with`$.getScript`. The `why` is another long story.
Majid
@Nick Craver: I'm lost for words, all I can say is, this is brilliant! I used the second method (parseInt), and it's working as expected. Thanks a million!
Majid
A: 

Try using a string rather than a number.

var my_min = "3";
$('#staylength').val(my_min);
Kinlan
As suggested by Sarfraz Ahmed I have tried `$('#staylength').val('' + my_min);` which does the same, but the problem is still there.
Majid
That's not going to make any difference.
Pointy
A: 

try

$('#staylength option[value='+my_min+']').attr('selected','selected'); 
// instead of $('#staylength').val('' + my_min);

but would be nice if you do this (requirement is Jquery 1.4 )

function apply_restriction(i) {
    var my_index = parseInt(i);
    var my_option;
    var my_min = min_stay[my_index];
    $('#staylength option').each(function(){
        $(this).attr('disabled',function(){
            return this.value < my_min;
        });
    });
    $('#staylength option[value='+my_min+']').attr('selected','selected');
    alert($('#staylength').length);
}
Reigel
That's also a good idea. In fact he can just put "selected" right in the HTML he's building!
Pointy
See my answer, this is not the issue...and that's a *really* long way of writing `$('#staylength').val(my_min);`, and actually your longer way is more error-prone, what if my `my_min = ']['`?
Nick Craver
cool nick! thanks!.. you're really good at this stuff... and I'm learning much from you..
Reigel