mozilla

Revision 129230 of removeAllItems

  • Revision slug: Talk:XUL/Method/removeAllItems
  • Revision title: removeAllItems
  • Revision id: 129230
  • Created:
  • Creator: Konijn
  • Is current revision? Yes
  • Comment page created, 151 words added

Revision Content

This page has no content. Enrich MDC by contributing.

So,

this code :

var o = document.getElementById( "Category" );

 

o.removeAllItems();

o.appendItem( "1d", "1" , "2a");

o.appendItem( "2d", "2" , "2b");

o.appendItem( "3d", "3" , "2c");

o.appendItem( "4d", "4" , "2d");

will completely freeze the menulist "Category", I guess thats because the old selected value is no longer a valid menuitem.

The only way to code around this that I found is to set the selectedIndex to a valid menuitem. Of course I did not want any value to be preselected, I wanted a visual clue that the user needs to make a decision. So I ended up with this:

 

var o = document.getElementById( "Category" );

o.removeAllItems();

o.appendItem( "Select a category", "1" , "2a");

o.appendItem( "1d", "1" , "2a");

o.appendItem( "2d", "2" , "2b");

o.appendItem( "3d", "3" , "2c");

o.appendItem( "4d", "4" , "2d");

o.selectedIndex = 0;

o.removeItemAt( 0 );

This works..

TjD

Revision Source

<p>This page has no content. Enrich MDC by contributing.</p>
<p>So,</p>
<p>this code :</p>
<p style="margin-left: 120px; ">var o = document.getElementById( "Category" );</p>
<p>
 </p><p>o.removeAllItems();</p>
<p>o.appendItem( "1d", "1" , "2a");</p>
<p>o.appendItem( "2d", "2" , "2b");</p>
<p>o.appendItem( "3d", "3" , "2c");</p>
<p>o.appendItem( "4d", "4" , "2d");</p>

<p>will completely freeze the menulist "Category", I guess thats because the old selected value is no longer a valid menuitem.</p>
<p>The only way to code around this that I found is to set the selectedIndex to a valid menuitem. Of course I did not want any value to be preselected, I wanted a visual clue that the user needs to make a decision. So I ended up with this:</p>
<p>
 </p><p>var o = document.getElementById( "Category" );</p>
<p>o.removeAllItems();</p>
<p>o.appendItem( "Select a category", "1" , "2a");</p>
<p>o.appendItem( "1d", "1" , "2a");</p>
<p>o.appendItem( "2d", "2" , "2b");</p>
<p>o.appendItem( "3d", "3" , "2c");</p>
<p>o.appendItem( "4d", "4" , "2d");</p>
<p>o.selectedIndex = 0;</p>
<p>o.removeItemAt( 0 );</p>

<p>This works..</p>
<p>TjD</p>
Revert to this revision