Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hiding option in select list

Status
Not open for further replies.

qwertyjjj

Programmer
May 27, 2005
70
GB
Is there a way to hide an option in a select list with javascript?
All code I have used so far just removes options but this messes about with the index numbers and stuff.
Ideally, I'd like to give each option an id and then decide whether to hide it or not then make it visible again at another time?
 
What you're proposing is not possible. To make the item in the dropdown not appear you'll have to remove it from the list.

One alternative (and take note that I'd never do this) is to change the font color on that particular option to white so that it is basically invisible to the user. However, they will still be able to click to select it - so it's a very bad solution. I'm afraid it's about the best you're gonna come up with though....

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
AFAIK this know I have never seen this done. After some google searches because I was curious google provided some articles that explained that most browsers support hiding option elemnts with css expect for ie. Here is the best info I was able to find
.

I think since you need to keep the options ordered and just want to display them based on some other criteria you may be able to make an object to hold your options and their corresponding display value. Loop through your object to create your select list. This will require you to rebuild your select list every time you update the data object.
I'm interested to see what others may post, as I'm sure this option may not be the best.
 
So, the best solution is to remove relevant options then when I want to rebuild the drop down, I wipe out the select list and rebuild from, for example, an array ?
 
qwertyjjj said:
So, the best solution is to remove relevant options then when I want to rebuild the drop down, I wipe out the select list and rebuild from, for example, an array ?

That would be my chosen solution.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
I would make an array with all options and then build the SELECT with from that array with DOM everytime you want to add or remove items. In addition you can use the "value" property on the OPTIONs for saving the static index number..

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top