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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript "Hide" Select options not working IE

Status
Not open for further replies.

jcandeli

Programmer
Dec 28, 2005
7
US
Thank you in advance for any help you may be able to provide!

I have created a simple example of some javascript that hides and unhides options in an HTML select box.

For some reason this code only seems to work in Firefox and not IE.

Since the select menu on my live site is dynamically created and potentially with many (Hundred) items, I cannot give each option in the select menue a unique ID and refernce it that way.

If you tell me how to get this simple example to work in IE you will be my hero! :D

<html>
<head>
<script language=javascript>
function hideMenuItem() {
var menu = document.getElementById("selectMenu");
menu.options.item(2).style.display = "none";
}
function resetMenuItems() {
var menu = document.getElementById("selectMenu");
menu.options.item(2).style.display = "block";
}
</script>
</head>
<body>
<form>
<select multiple id="selectMenu">
<option value ="1">1</option>
<option value ="2">2</option>
<option value ="3">3</option>
<option value ="4">4</option>
</select>
<input type=button value="Hide Menu option" onclick="hideMenuItem();">
<input type=button value="Reset Menu options" onclick="resetMenuItems();">
</form>
</body>
</html>
 
test for browser? not 100%

function hideMenuItem() {
var menu = document.getElementById("selectMenu");
if (navigator.appVersion.indexOf("MSIE") != -1){
menu.options.item(2).style.display = "none";
}else if (document.getElementById) {
menu.options.item(2).style.visibility = "hidden";
}else if(navigator.appName == "Netscape"){
menu.options.item(2).visibility = "hidden";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top