1

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?

4
  • 2
    maybe you could post a bit more code to give a clearer picture of what your asking. Commented Jun 25, 2009 at 10:57
  • I just want the selectbox to be cleared. That's all Commented Jun 25, 2009 at 11:00
  • Think the community of SO would like to have some more code. But if I work with what I got I would say that you should try to locate 'var gs = [...];' and make sure that it´s a reference to the select-element. The HTML DOM Select element has an options-array but I can´t recall that there exists a native remove-method on it. Commented Jun 25, 2009 at 11:03
  • 1. how are you searching - pressing a button? 2. how are you getting the "gs" reference? 3. Do you want the contents of the the select box to be removed or Do you want the currently selected value to be not selected? 4. How is clearScholen being called? Commented Jun 25, 2009 at 11:08

2 Answers 2

2

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);     
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.