Walter,

1) "new Object()" could be easily replaced with object literal - "{ }"
2) window.comboBackup is a not a good idea (i.e. polluting global
object)
3) string concatenation can be replaced with either explicit element
creation or a template (if performance doesn't matter)

I haven't tested it but give it a shot:

document.observe('change', function(e) {
  var element = e.element();

  if (!$F(element) == 'Other...') return;

  if (!element._comboBackup)
    element._comboBackup = { };

  element._comboBackup = element.replace(
    new Element('input', {
      type: 'text',
      id: element.id
    }).setStyle({ width: element.getWidth() + 'px' });
  );

  element.activate().observe('blur', function(e) {
    var t = e.element();

    if ($F(t) == t.defaultValue) {
      t.replace(element._comboBackup);
      element.options.selectedIndex = 0;
    }
  });
})

Best,
kangax


On Apr 19, 11:22 am, Walter Lee Davis <[EMAIL PROTECTED]> wrote:
> I wrote this function to make "combo boxes" out of ordinary select
> fields.
>
> document.observe('dom:loaded',function(){
>    var combo = function(evt){
>      var elm = evt.element();
>      if ($F(elm) == 'Other...'){
>        var w = elm.getWidth() + 'px';
>        var theId = elm.id;
>        //following var should be all one line!
>        var txt = '<input type="text" name="' + elm.name + '" id="' +
> theId + '" style="width:' + w + ';" />';
>        if(!window.comboBackup) window.comboBackup = new Object();
>        window.comboBackup[theId] = elm.replace(txt);
>        $(theId).activate().observe('blur',function(e){
>          var t = e.element();
>          if($F(t) == t.defaultValue){
>            t.replace(window.comboBackup[theId]);
>            $(theId).options.selectedIndex = 0;
>          }
>        });
>      }
>    }
>    $$('select.combo').invoke('observe','change',combo);
>
> });
>
> (demo here)
>
> http://scripty.walterdavisstudio.com/combo.html
>
> Looking at it, I can see that it's not up to modern coding standards
> -- it doesn't look much like the other examples I see flying around on
> this list. But I am not sure where to start cleaning it up and making
> it better. It works, gets the job done, but there are some places
> where I am simply concatenating a big string, and that feels like I'm
> missing an opportunity to do a more elegant job.
>
> Can anyone offer any suggestions?
>
> Also, how would you go about adding the "Other..." option using the
> same script? It just occurred to me that if you did this, it would
> degrade more gracefully in the absence of script if that were the
> case. I recall reading quite a few examples on this list where IE was
> unwilling to add options to a select without some really heavy blows
> to the head.
>
> Thanks in advance,
>
> Walter
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to rubyonrails-spinoffs@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to