Link to home
Start Free TrialLog in
Avatar of _JHL_
_JHL_

asked on

How can clear all options and optgroup of select?

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
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

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
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

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
Avatar of _JHL_
_JHL_

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of nabsol
nabsol
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial