Here's an implementation that works in all the browsers I've checked:

var combinator = function(){
        $$('select.combo').each(function(elm){
                var elm = $(elm);
                var texts = ['Choose...'];
                var opts = [''];
                var sel = 
(elm.options[elm.options.selectedIndex].defaultSelected) ?  
elm.options.selectedIndex : -1;
                for (var i=0; i < elm.options.length; i++) {
                        opts.push(elm.options[i].value);
                        texts.push(elm.options[i].text);
                };
                texts.push('Add new...');
                opts.push('Add new...');
                elm.options.length = 0;
                texts.each(function(el,idx){
                        elm.options[idx] = new Option(el, opts[idx], (idx == 
0), false);
                });
                elm.options.selectedIndex = (sel + 1);
                elm.observe('change',function(evt){
                        if(this.value == 'Add new...'){
                                if(!window.bak) window['bak'] = {};
                                window.bak[this.id] = this;
                                var ti = new Element('input', 
{type:'text',name:this.name,id:this.id});
                                this.replace(ti);
                                ti.focus();
                                ti.observe('blur',function(evt){
                                        if(this.getValue() == '') {
                                                
this.replace(window.bak[this.id]);
                                                
$(this.id).options.selectedIndex = 0;
                                        }
                                });
                        }
                })
        });
}


I use this to turn select controls into combo boxes, where you can add  
a value not found in the list by choosing Add new...

I believe what you need to look at is whether the selectedIndex is  
also the defaultSelected. The picker will always show whatever is the  
selectedIndex, but on a reload, it will show whatever is the  
defaultSelected.

Walter

On Dec 10, 2009, at 11:11 AM, Ruben. D. wrote:

> I've tried in Firefox and Opera and the result is the same.
>
> -- 
> Rubén Dávila Santos.
> http://rubenonrails.com
>
> --
>
> You received this message because you are subscribed to the Google  
> Groups "Prototype & script.aculo.us" group.
> To post to this group, send email to prototype-scriptaculous@googlegroups.com 
> .
> To unsubscribe from this group, send email to 
> prototype-scriptaculous+unsubscr...@googlegroups.com 
> .
> For more options, visit this group at 
> http://groups.google.com/group/prototype-scriptaculous?hl=en 
> .

--

You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.


Reply via email to