Loading ...
Sorry, an error occurred while loading the content.

Re: [ydn-javascript] Object 'class' in IE vs FF

Expand Messages
  • Gopalarathnam Venkatesan
    ... Hi Mike: You can use document.createElement() to create the option element on the fly. Here s a quick example for the same:
    Message 1 of 3 , May 3, 2007
    • 0 Attachment
      Mike Sexter wrote:
      >
      >
      > I'm developing my YUI app and have the need to replace the options on a
      > select. The following code is what I'm using to do this:
      >
      >
      >
      > replaceOptions = function (id, opts){
      >
      > var sel = document.getElementById(id);
      >
      > alert(sel);
      >
      > if (sel) {
      >
      > var i = sel.length;
      >
      > while (i > 0) {
      >
      > sel.remove(--i);
      >
      > }
      >
      > for (i=0; i<opts.optionSet.length; i++) {
      >
      > var opt = new Option();
      >
      > opt.value = opts.optionSet[i].value;
      >
      > opt.text = opts.optionSet[i].text;
      >
      > sel.add(opt, null);
      >
      > }
      >
      > sel.selectedIndex = 0;
      >
      > }
      >
      > }
      >
      >
      >
      > This works fine on Firefox but fails on line 13 on IE because of a 'Type
      > mismatch'.
      > The alert on line 3 outputs [object HTMLSelectElement] on FF but only
      > [object] on IE.
      > Hence the problem. Any help on how to deal with this would be appreciated.
      >

      Hi Mike:

      You can use document.createElement() to create the "option" element on
      the fly.

      Here's a quick example for the same:
      http://gopalarathnam.com/examples/javascript/dynseladd.html

      But as someone has already mentioned in this list, creating the "select"
      and the "option" elements as an HTML string and dumping them into the
      container is much faster than using DOM methods.

      You can see the "Writing Efficient JavaScript" video available YUI Theater:

      http://developer.yahoo.com/yui/theater/

      The slides/handouts are available from my web site:

      Slides -
      http://gopalarathnam.com/talks/Writing_Efficient_JavaScript_F2ESummit2007.pdf
      Handouts -
      http://gopalarathnam.com/talks/Writing_Efficient_JavaScript_F2ESummit2007_Handout.pdf

      --
      Gopalarathnam Venkatesan

      http://gopalarathnam.com/
    Your message has been successfully submitted and would be delivered to recipients shortly.