ok, heres some sample code that demonstrates the problem. if i click the button in firefox, the first option disappears. if i click the button in chrome, nothing happens, or rather if i inspect the first option, it does have the attribute "style='display:none'" but the option itself on the html page is not hidden.
<form>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input type="button" onclick="document.getElementsByTagName('option')[0].style.display='none'" value="hide option 1">
</form>
why doesn't this work in chrome?
For deleting of one option from select form may be used jQuery's $().eq().remove() or $().remove().
Similar was my deleting of one table row from table:
where:
say that removed element will be table row (tr) from table enclosed in form (form table).
say that removed will be the second element (element with index number 1)
say that element will be removed.
But when this should be used on option, then all needed would be (for example):
because it is simplier to find option by value than by index number (unless you would have more ioption with the same value - but it is nonsense, to have such options). Using of index number is good if you have not any else that you may use to find removed element.
Also you may, of course, add form name or form name and select name (or id, or class) into element identification
but the first option is better - and more logical, due to server-side processes, where you need to use attribute name instead id or class.
The workaround is to remove the
option
elements in response to your event and add them back if and when they are needed. IIRC, IE will not allow you to set thedisplay
tonone
onoption
elements. I would advise storing the removed elements in an array so that you can easily add them back in.As my solution (asp.net) .I try to do by that way. 1.Create a dropdowlist will all ListItems 2.Using javscript to add or remove ListItems (Note: add option need same value and text in asp:dropdowlist items)
Solution:
The select box changed to 2 size (like multiple), but working. This trick don't working under safari :(
I know this is now old, but you could - especially if you are using jQuery change parent.
You probably have to remove the
<option>
instead of 'hiding' it. If it's not a solution for you, try disabling it.PS: You might want to reconsider using
getElementsByTagName('option')
, can get messy if you have more<select>
elements.