sanders
sanders

Reputation: 10898

remove() function not working in javascript

I have an html select box and a search field (input type)

When I Search for something new , a javascript function first clears the selectfield

But Javascript gives the following error:

gs.options.remove is not a function

This is the function

 function clearScholen()
  {
    for (var i=gs.options.length;i>=0;i--)
    {
      gs.options.remove(i); 
    }  
  }

and the value of gs =

<select style="width: 420px; height: 150px;" name="selectbox" size="5">

Whats going wrong?

Upvotes: 1

Views: 3444

Answers (2)

Paul Rowland
Paul Rowland

Reputation: 8352

I guess in your example "gs" doesnt reference a selectbox.

Removing all the options

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

Removing Selected Options

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

Upvotes: 2

Jonathan Fingland
Jonathan Fingland

Reputation: 57197

If I understand you correctly, you want to clear the search field (which is working) and reset the select drop down.
If that's the case, you want:

gs.selectedIndex = -1;

e.g.

function clearScholen()
  {
       gs.selectedIndex = -1;

  }   

assuming that gs is previously defined

Upvotes: 2

Related Questions