views:

612

answers:

3

Internet Explorer 8 and 7 fails with error when i update the select box with new option and choose the selected option. In other browsers this works fine and without errors. I use Mootools 1.2.3.

<?php
    $getdestiny=$_GET['dest'];
    $getcountry=$_GET['countr'];

    print "<script type='text/javascript'>
        window.addEvent('domready', function() {

       var countrh=$getcountry;
       var desth=$getdestiny;

       if (countrh==4){
             $('destination').options.length=0; //error fails here   
             var opt0 = $('destination').options[0] = new Option('Сhoose Destination',0);
             var opt1 = $('destination').options[1] = new Option('London-Aberdeen','1');

              var len = $('destination').options.length;
              if (desth < len){
             opt$getdestiny.setProperty('selected','selected'); //or here

        else {
        //do nothing
        }
    });
    </script>"

Can it be because i use double-assignment for vars opt* or what can it be else? Advise me please.

A: 

In the past we have used the following syntax successfully to mark a select option element as "selected":

option.selected = true;
Ben Foster
opt$getdestiny.option.selected = true; gives an error Message: 'option' - is null or not an object
moogeek
function removeAllOptions(el) { var el=$(el); if(el.get('tag')!='select') return el; for(var i=el.options.lelngth-1;i>=0;i--) el.remove(i); return el; }same thing...
moogeek
I'm not too familiar with PHP.. but I'm assuming "opt$getdestiny" is the option element? In that case wouldn't it be "opt$getdestiny.selected = true;"?
Ben Foster
Also, to get the number of options in a select, instead of "el.options.length" do "el.length".
Ben Foster
seems to be the similiar error with this fixes...
moogeek
A: 

in mootools 1.2, setProperty() got deprecated. the prototype is now just element.set(prop, value); - one setter and one getter (element.get()) for anything :)

Dimitar Christoff
A: 

It looks like you just have a syntax error here where you're getting your 2nd error at.

if (desth < len){
   opt$getdestiny.setProperty('selected','selected'); //or here
else {
  //do nothing
}

is missing a closing } for the first part of the if statement. Should be like this:

if (desth &lt; len){
   opt$getdestiny.setProperty('selected','selected'); //or here
}else {
  //do nothing
}

ADDENDUM:

Ok your problem is that you're trying to use a MooTools function on an element that's not wrapped in MooTools. You can fix this by wrapping it in a $() function like this:

$(opt$getdestiny).set('selected','selected');

I also typically just set selected = true so you can try that as well. Make sure to use true, not the string 'true'.

Shawn Steward
I updated my Answer post with more details, this should clear up the problem you're getting.
Shawn Steward
it's fantastic!!! A lots of Thanks And a Big Respect, Shawn!!!
moogeek
No problem, glad to help!
Shawn Steward