IE/XP Problem setting option element to selected

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomastk@hotmail.com

    IE/XP Problem setting option element to selected

    Hi,
    In the following script, I am trying to set selection to a select
    option element, that is newly created within the script. It works fine
    on IE installations on Windows 2000 and some XP machines. But on some
    XP machines, the selection doesn't happen and it defaults to the first
    element in the options array. Has anybody come across this problem ?
    Any known workarounds?

    Thanks
    Thomas.
    =============== =============== =====

    <HTML>
    <HEAD>
    <META NAME="GENERATOR " Content="Micros oft Visual Studio 6.0">
    <TITLE></TITLE>
    </HEAD>
    <BODY>

    <P><INPUT id=text1 name=text1 onchange="javas cript:mytesting (this);">
    <SELECT id=select1 style="WIDTH: 264px" name=select1>
    <OPTION selected value="testing 1">testing 1</OPTION>
    <OPTION value="testing 2">testing 2</OPTION>
    <OPTION value="testing 3">testing 3</OPTION>
    <OPTION value="testing 4">testing 4</OPTION>
    <OPTION value="testing 5">testing 5</OPTION>
    </SELECT><INPUT id=button1 type=button value="delect new added"
    name=button1></P>
    </BODY>
    </HTML>
    <script language="JavaS cript" >
    function mytesting()
    {
    var fsDropDown = document.getEle mentById("selec t1");
    if (fsDropDown !=null) {
    var tempFS = fsDropDown.valu e;
    //var selIdx = fsDropDown.sele ctedIndex;
    var opt = document.create Element("OPTION ");
    fsDropDown.opti ons.add(opt);
    tempFS = tempFS+"*";
    opt.innerText = tempFS;
    opt.value = tempFS;
    //opt.selected = true;

    fsDropDown.opti ons[fsDropDown.opti ons.length-1].selected=true;
    fsDropDown.sele ctedIndex = fsDropDown.opti ons.length-1;
    }
    }
    </script>

  • RobG

    #2
    Re: IE/XP Problem setting option element to selected

    thomastk@hotmai l.com wrote:[color=blue]
    > Hi,
    > In the following script, I am trying to set selection to a select
    > option element, that is newly created within the script. It works fine
    > on IE installations on Windows 2000 and some XP machines. But on some
    > XP machines, the selection doesn't happen and it defaults to the first
    > element in the options array. Has anybody come across this problem ?
    > Any known workarounds?
    >
    > Thanks
    > Thomas.
    > =============== =============== =====
    >
    > <HTML>
    > <HEAD>
    > <META NAME="GENERATOR " Content="Micros oft Visual Studio 6.0">
    > <TITLE></TITLE>
    > </HEAD>
    > <BODY>
    >
    > <P><INPUT id=text1 name=text1 onchange="javas cript:mytesting (this);">
    > <SELECT id=select1 style="WIDTH: 264px" name=select1>
    > <OPTION selected value="testing 1">testing 1</OPTION>
    > <OPTION value="testing 2">testing 2</OPTION>
    > <OPTION value="testing 3">testing 3</OPTION>
    > <OPTION value="testing 4">testing 4</OPTION>
    > <OPTION value="testing 5">testing 5</OPTION>
    > </SELECT><INPUT id=button1 type=button value="delect new added"
    > name=button1></P>
    > </BODY>
    > </HTML>
    > <script language="JavaS cript" >
    > function mytesting()
    > {
    > var fsDropDown = document.getEle mentById("selec t1");
    > if (fsDropDown !=null) {
    > var tempFS = fsDropDown.valu e;
    > //var selIdx = fsDropDown.sele ctedIndex;
    > var opt = document.create Element("OPTION ");
    > fsDropDown.opti ons.add(opt);
    > tempFS = tempFS+"*";
    > opt.innerText = tempFS;
    > opt.value = tempFS;
    > //opt.selected = true;
    >
    > fsDropDown.opti ons[fsDropDown.opti ons.length-1].selected=true;
    > fsDropDown.sele ctedIndex = fsDropDown.opti ons.length-1;
    > }
    > }
    > </script>
    >[/color]

    IE has problems with options. To set all the properties on one go, use
    something like:

    var opt = new Option( text, value, defaultSelected , currentSelected );

    where:
    text is the option text as a string,
    value is the option value as a string,
    defaultSelected is a boolean (true or false),
    currentSelected is a boolean (true or false),

    There is a thread worth reading here:

    <URL:http://groups.google.c o.uk/group/comp.lang.javas cript/browse_frm/thread/f1037605dfb1a9c 0/07f4f9df6cd3cdc 1?q=new+option( +value+text&rnu m=20&hl=en#07f4 f9df6cd3cdc1>




    --
    Rob

    Comment

    • ASM

      #3
      Re: IE/XP Problem setting option element to selected

      thomastk@hotmai l.com wrote:[color=blue]
      > Hi,
      > In the following script, I am trying to set selection to a select
      > option element, that is newly created within the script. It works fine
      > on IE installations on Windows 2000 and some XP machines. But on some
      > XP machines, the selection doesn't happen and it defaults to the first
      > element in the options array. Has anybody come across this problem ?
      > Any known workarounds?[/color]

      see there :
      <http://aliasdmc.free.f r/dom_javascript_ html/javascript_html _select_add.htm l>

      and see also bellow old JS that works
      [color=blue]
      > <HTML>
      > <HEAD>
      > <META NAME="GENERATOR " Content="Micros oft Visual Studio 6.0">[/color]

      ha ! ben bravo !
      [color=blue]
      > <TITLE></TITLE>[/color]

      <script type="text/javascript">
      [color=blue]
      > function mytesting()
      > {
      > var fsDropDown = document.getEle mentById("selec t1");
      > if (fsDropDown !=null) {[/color]

      var tempFS = fsDropDown.opti ons[fsDropDown.sele ctedIndex].value;
      fsDropDown.leng th++;
      var opt = fsDropDown.opti ons[fsDropDown.leng th-1]
      opt.value = opt.text = tempFS = tempFS+"*";
      opt.selected = 'selected';
      if(document.all ) opt.selected = true;
      [color=blue]
      > }
      > }
      > </script>
      > </HEAD>
      > <BODY>
      >
      > <P><INPUT id=text1 name=text1 onchange="javas cript:mytesting (this);">
      > <SELECT id=select1 style="WIDTH: 264px" name=select1>
      > <OPTION selected value="testing 1">testing 1</OPTION>
      > <OPTION value="testing 2">testing 2</OPTION>
      > <OPTION value="testing 3">testing 3</OPTION>
      > <OPTION value="testing 4">testing 4</OPTION>
      > <OPTION value="testing 5">testing 5</OPTION>
      > </SELECT><INPUT id=button1 type=button value="delect new added"
      > name=button1></P>
      >
      > </BODY>
      > </HTML>[/color]

      --
      Stephane Moriaux et son [moins] vieux Mac

      Comment

      • RobG

        #4
        Re: IE/XP Problem setting option element to selected

        ASM wrote:
        [...][color=blue]
        >
        > var tempFS = fsDropDown.opti ons[fsDropDown.sele ctedIndex].value;
        > fsDropDown.leng th++;
        > var opt = fsDropDown.opti ons[fsDropDown.leng th-1]
        > opt.value = opt.text = tempFS = tempFS+"*";
        > opt.selected = 'selected';
        > if(document.all ) opt.selected = true;[/color]


        var tempFS = fsDropDown.opti ons[fsDropDown.sele ctedIndex].value;
        var newOpt = new Option( tempFS+'*', tempFS+'*', false, true );
        fsDropDown.opti ons[fsDropDown.opti ons.length] = newOpt;

        [...]

        --
        Rob

        Comment

        • thomastk@gmail.com

          #5
          Re: IE/XP Problem setting option element to selected

          Tried both the suggestions, but the problem is still reproducible on
          the problem XP machines, as with the js/html I had posted.

          Thanks
          Thomas.

          Comment

          Working...