views:

64

answers:

3

I have the following code:

<select>
    <option value="0">Option 0</option>.
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

When you click on select I wish the first item to be no longer listed. Also it must work on all browsers. How can this be done?

Thanks.

A: 

See if select.remove() method works. It is supposed to be part of w3c standards but not sure if its implemented in all browsers.

Salman A
A: 

hmmmm not sure that editing collections like that is going to work 100% of the time. If you dont supply a value for the select item then you should be able to throw a validation error as no "Value" is selected.

<select> 
    <option value="">Select a value...</option> 
    <option value="1">Option 1</option> 
    <option value="2">Option 2</option> 
</select>

then use one of the JQuery validation libraries to validate it.

Mauro
I don't need to validate. I want to show the first option only for information.
Emanuel
It's an `<option>`. It isn't designed to provide information (other than "This is something you can pick") to the user.
David Dorward
A: 
<select id="selectBox"
  onFocus="javascript:nullOpt=document.getElementById('nullOpt');
           (undefined != nullOpt) ?
           document.getElementById('selectBox').removeChild(nullOpt) :
           true;"
  onBlur="javascript:insertBefore(
                          nullOpt,
                          document.getElementById('selectBox').firstChild
                     );">
  <option id="nullOpt">Please select...</option>
  <option>val1</option>
  <option>val2</option>
  <option>val3</option>
</select>

​EDIT: Added newly requested functionality, sort of.

mVChr
Unless you select any item and "select" tag lose focus how to redisplay first element?
Emanuel
You didn't mention that you wanted to ever redisplay it and I assumed that if they focused on the select box then they would know to select something at that point. You could assign nullOpt to another variable and re-add it onBlur if you wanted.
mVChr
fixed to show how to do that, but I think this will clear their selection. You might want to re-think your functionality spec.
mVChr