FIX: Removing Non-Visible Items from an HTML SELECT Box Causes General Protection Fault (GPF) (239657)



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 5

This article was previously published under Q239657

SYMPTOMS

When removing items from a <SELECT MULTIPLE> tag, removing more selected items than are viewable at once in the select box causes a GPF.

RESOLUTION

Create a temporary list of the selected elements, deselect all of them, and then remove the elements per the temporary list.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Internet Explorer 5.01.

MORE INFORMATION

This is a generic example of the code used in such an instance:

<FORM NAME="myform">
<SELECT NAME="widget" SIZE="3" MULTIPLE>
<OPTION>111111</OPTION>
<OPTION>222222</OPTION>
<OPTION>333333</OPTION>
<OPTION>444444</OPTION>
<OPTION>555555</OPTION>
<OPTION>666666</OPTION>
<OPTION>777777</OPTION>
<OPTION>888888</OPTION>
<OPTION>999999</OPTION>
</SELECT>
<INPUT TYPE="BUTTON" VALUE="Crash or not" ONCLICK="RemoveSelectedItems()">
</FORM>
				
Here is the script code to remove the selected items that causes GPF.
<SCRIPT LANGUAGE="JavaScript">

   function RemoveSelectedItems()
   {
      var i

      for ( i = document.myform.widget.options.length - 1; i >= 0; --i )
      {
         if (  document.myform.widget.options[ i ].selected )
         {
           document.myform.widget.options[ i ] = null
         }
      }
   }

</SCRIPT>
				
To reproduce the problem, you need to select at least four items in this sample and then click the "Crash or not" button.

The root of the problem is removing the selected items. The workaround is to deselect the items, and save the list of selections in a temporary array.

The following is an example of how this might be coded:
<SCRIPT LANGUAGE="JavaScript">

   function RemoveSelectedItems()
   {
      var i, cnt
      var mywidget
      var selArray = new Array()

      myWidget= document.myform.widget

      for ( i = myWidget.options.length - 1; i >= 0; --i )
      {
         if (  myWidget.options[ i ].selected )
         {
            myWidget.options[i].selected = false
            selArray[selArray.length]=i
            //document.myform.widget.options[ i ]=null
         }
      }

      for ( i =  0 ; i <= selArray.length; i++ )
      {
         myWidget.options[ selArray[i] ]=null
      }
   }

</SCRIPT>
				

REFERENCES

For additional information about issues with Select boxes, please click the article number below to view the article in the Microsoft Knowledge Base:

237831 PRB: Problem Adding New Option to Select Box in Another Frame in Internet Explorer 5

For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbBug kbDHTML kbFAQ kbfix KB239657