Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can clear all options and optgroup of select?

Posted on 2006-04-11
8
Medium Priority
?
4,461 Views
Last Modified: 2008-01-09
Hi,

I want to clear a Select elements. I haven´t any proble with the options, but with the Optgroup I have a problem.

When the page loads, the select list fills with some values, that are grouped. The I have a button that change those values with it´s key, and then I refill the select with the new values. Here the Optgroup is repeated.... I don´t want to replace the select wqith a new elemnt, because I create this select at first time from a class with some characteristics and functions assigned.

So, how I can clear all the optgroup??

Thanks

Hugo
0
Comment
Question by:_JHL_
  • 4
6 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 16427501
You mean remove the items from inside the select?

I don't get what you mean by "optgroup" :-(

If it is what I said, then:

   select.options.length = 0 ;

will clear them
0
 
LVL 6

Expert Comment

by:nabsol
ID: 16427509
Hi

Hope this helps

To remove all:
function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
selectbox.remove(i);
}
}



///////////Or ///////////

endSelect = document.getElementById('endSelect');
/* Clear out the current options */
for (i = 0; i < endSelect.options.length; i++) {
    endSelect.options[i] = null;
}


By Nab

0
 
LVL 6

Expert Comment

by:nabsol
ID: 16427527
Hi

To remove only selected options

function removeOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
if(selectbox.options[i].selected)
selectbox.remove(i);
}
}

By Nab
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:_JHL_
ID: 16427578
nasbol....
Thanks, I actually use that function to clear the options, but my problem comes with the optgroup, they don´t deleted.... I want to clear all inclusive the optgroup

Thanks...
0
 
LVL 6

Expert Comment

by:nabsol
ID: 16427630
0
 
LVL 6

Accepted Solution

by:
nabsol earned 500 total points
ID: 16427649
Hi

This is the code from that link

<html>
<head>
   <script type='text/javascript'>

      var fruitObj = new Object();
      fruitObj['apple'] = 'crunchy';
      fruitObj['pear'] = 'crunchy';
      fruitObj['orange'] = 'squishy';
      fruitObj['lemon'] = 'squishy';
      fruitObj['lime'] = 'squishy';
      fruitObj['banana'] = 'mushy';

      function LoadFruits()
      {
         try
         {
            var fruitList = document.getElementById('Fruit');

            var textureName = '';
            for (var prop in fruitObj)
            {
               if (textureName != fruitObj[prop])
               {
                  textureName = fruitObj[prop];
                  var textureGroup = document.createElement
('optgroup');
                  textureGroup.label = textureName;
                  fruitList.appendChild(textureGroup);
               }

               var fruitName = document.createElement('option');
               fruitName.value = prop;
               fruitName.innerText = prop;
               textureGroup.appendChild(fruitName);
            }
         }
         catch(er)
         {
            alert(er);
         }
         alert(document.all('Fruit').innerHTML)
      };



      function RemoveFruitTexture(texture)
      {
         try
         {
            var fruitList = document.getElementById('Fruit');
            var eachGroup = fruitList.firstChild;
            while (texture != eachGroup.label)
            {
               alert(eachGroup.label)

               eachGroup = eachGroup.nextSibling;
            }
            fruitList.removeChild(eachGroup)
         }
         catch(er){}

      }

      function RemoveGroups()
      {
         try
         {
            var fruitList = document.getElementById('Fruit');

            var initialLength =  fruitList.length;
            for (var o = 0; initialLength > o; o++)
            {
               var cloneOption = fruitList.options[o].cloneNode(true);
               fruitList.appendChild(cloneOption);
            }


            var eachGroup = fruitList.firstChild;
            while ('OPTGROUP' == eachGroup.tagName)
            {
               lastGroup = eachGroup;
               eachGroup = lastGroup.nextSibling;
               fruitList.removeChild(lastGroup)
            }
         }
         catch(er){}
         alert(document.all('Fruit').innerHTML)

      }

   </script>
</head>

<body>
   <form id='foodForm'>
      <select id='Fruit'> </select>
      <br />
      <input type='button' value='Show Fruits' onclick='LoadFruits()'>
      <br />
      <input type='button' value='No Squishy!'
onclick='RemoveFruitTexture("squishy")'>
      <br />
      <input type='button' value='Just Fruits!' onclick='RemoveGroups
()'>

   </form>
</body>
</html>

That is a nice example if helps

By Nab
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question