Removing options from a List Box in client side Javascript

Removing options from Listbox We can remove the options from a list box by using client side JavaScript. You can see the article explaining how to add options to a list box using JavaScript. Here we will discuss on how to remove option on selection and removing all the options in one go. We will use different functions and connect them to buttons to execute them. Here are some functions and there uses in removing the options. You can see the demo of this program here.

While the page loads we will try to populate the list box by using onload event of the body tag. The list box will be filled with data at the time of display to the visitors. The detail on how to add options to list box you can get here. So we will go to the next step

Removing all the options

To remove all the options from the list box we will loop through all the elements of the list box and remove one by one. We will use for loop to loop from 0 to selectbox.options.length-1. The total length ( or the number of elements ) of the array we can get by using selectbox.options.length and to this we are subtracting 1 as the first elements starts from 0 ( not from 1 ). Here is the function

Here is the demo of the script

function removeAllOptions(selectbox)
	var i;
The for loop will remove all the elements with the value of i changing from last element of the array to first element of the array.

Removing Selected Options

We can remove the options one by one or by selecting more than one option and then by pressing the button. Here also we will use the similar function like above but before deleting we will check if the option is checked or not. selectbox.options[i].selected will return true if the option is selected. This way we will check all the elements of the list box and if they are checked then we will add the command selectbox.options.remove(i); to remove that particular option from the list box. Here is the function.

function removeOptions(selectbox)
	var i;
Adding the options
There is a full tutorial on adding the options to list box. So here is the function.

function addOption(selectbox,text,value )
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

function addOption_list(selectbox){
addOption(document.drop_list.SubCat, "One","One");
addOption(document.drop_list.SubCat, "Two","Two");
addOption(document.drop_list.SubCat, "Three","Three");
addOption(document.drop_list.SubCat, "Four","Four");
addOption(document.drop_list.SubCat, "Five","Five");
addOption(document.drop_list.SubCat, "Six","Six");

Now we will see how the select box with the form and buttons are placed in the body are of the page. Each button is connected to one function with on click event handler. See here

 <FORM name="drop_list" action="yourpage.php" method="POST" >
<SELECT id="SubCat" NAME="SubCat" MULTIPLE size=6 >
<input type=button onClick="removeOptions(SubCat)"; value='Remove Selected'>
<input type=button onClick="removeAllOptions(SubCat)"; value='Remove All'>
<input type=button onClick="addOption_list()"; value='Add All'> 



* indicates required
Subscribe to plus2net



    Thanks a lot. code is working fine in IE but not working with MOZILA. Please help me.


    Check the demo link, it is working fine in FireFox, Chrome & IE
    Manas Sinha


    Yes,It worked. Actually I used options.remove(i) so it was not removing all. Thanks...


    function removeAllOptions(selectbox) { selectbox.length = 0; }


    I have many list boxes in one form. How do i clear all the list in all the boxes?


    hi Achmend Get The All selectBox Ids in Javascript and and their Length ,then perForm Remove Method using For loop for all


    i have a problem. i want to sign in using the username, password and one option from the drop down box, there are two option and on both optin different page is open how can i do this plz help me


    I dont need remove all ,remove and buttons there.As soon as i select an item from the list it should get deleted.

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer