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!

Disable dropdown options 2

Status
Not open for further replies.

rmz8

Programmer
Aug 24, 2000
210
US
How can I disable the first two options in a dropdown list?

Thanks

Ryan ;-]
 
What? I guess you don't get what I mean. I have two options:

--SELECT AN OPTION--
+-+-+-+-+-+-

I want people not to be able to submit them. I know FrontPage had a function that could do this--but I don't use it, so that makes it tough.

Ryan ;-]
 
you could just add the disabled attribut to the tag. or to refrence that attribute from javascript:

document.formName.selectName.disabled=true;

also, you could just remove all of the options from the list:

document.formName.selectName.options.length=0;

although i am not sure about the second one, i do know that if it works, there will not be any way to put the options back in.


hope this helps
theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
What is the "disabled" attribute of the tag?

Ryan ;-]
 
disabled only works in ie. if you set disabled = true; it will make it so that you can't use the select box at all.

i know that there are ways to take options out and add options to a select box... i just don't remember how.

jaredn has a function that does this, i'm not sure if it's an ie only solution, knowing him, it probably is, but, i'll let him post it anyways. adam@aauser.com
 
That's aright. I really don't need the function. I just thought that there was a fucntion that could easily make it so that selection options couldn't be selected.

Ryan ;-]
 
document.formName.selectName.options.length=0;
document.formName.selectName.options.length=0;
document.formName.selectName.options.length=0;
document.formName.selectName.options.length=0;
document.formName.selectName.options.length=0;
document.formName.selectName.options.length=0;


didn't I say that once before?
theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
I don't want to remove options from the list. I simply want to make some options for show only. So that when the tried to submit the form on those specific options, it would alert the user that they have to select a valid option.

Ryan ;-]
 
Try this:

<select name=&quot;MySelect&quot;>
<option value=&quot;x&quot;>Pick something
<option value=&quot;x&quot;>----------------
<option>One
<option>Two
<option>Three
</select>
<input type=&quot;button&quot; value=&quot;Submit&quot; onClick=&quot;if(this.form.MySelect[this.form.MySelect.selectedIndex].value!='x'){this.form.submit()};&quot;>

Hope this helps....

<webguru>iqof188</webguru>
 
iq---

I have multiple selection boxes (that I want the same thing done to), so this makes that code tough to work with. Also, how do I make it so that the alert comes up? I should probably just create a function, right?

Ryan ;-]
 
Yes Ryan, I think it will be best to write a function. This is what I could come up with in five minutes:

<script language=&quot;Javascript&quot;>
function checkSelects(form) {

select1 = form.MySelect1
select2 = form.MySelect2

if(select1.value=='x') {
alert('Select a first number please!')
return false;
}
if(select2.value=='y') {
alert('Select a second number please!')
return false;
}
return true;
}
</script>

<form>
<select name=&quot;MySelect1&quot;>
<option value=&quot;x&quot;>Select first number
<option value=&quot;x&quot;>----------------
<option>One
<option>Two
<option>Three
</select>
<select name=&quot;MySelect2&quot;>
<option value=&quot;y&quot;>Select second number
<option value=&quot;y&quot;>----------------
<option>Four
<option>Five
<option>Six
</select>
<input type=&quot;Submit&quot; value=&quot;Submit&quot; onClick=&quot;return checkSelects(this.form);&quot;>

</form>

Your function should be something like this. Hope this helps... <webguru>iqof188</webguru>
 
Thanks so much iq! It works great. You got my vote--again.

Ryan ;-]
 
Ouch! I spoke too soon. It works well in IE, but of course NS encounters errors. I think it has to do with how the form is referenced within the code. It gives the following error:

&quot;frm is not defined&quot;

Ryan ;-]
 
That's strange, because I never typed &quot;frm&quot; in my code??! Are you sure you typed your own code correct? Please show your code so I can see what's wrong :)

<webguru>iqof188</webguru>
 
The form name is frm, so I thought I had to change it. I changed it back and it works! Thanks iq!

Ryan ;-]
 
By the way, do you really have an IQ of 188? :)

Ryan ;-]
 
<html>
<head>
<title>
disabled dropdowns
</title>
</head>
<body>
<form>
<select>
<option disabled>Please Pick a number
<option disabled>Please Pick a number
<option>One
<option>Two
<option>Three
</select>
</form>
</body>
</html>

You could always use that but it only works in Netscape 6/Mozilla. Its a standard tho -- so mabye MS will decide to implement them soon so we don't have to write javascripts for their broswer when we could just be using html . . . ===
Supports Mozilla and Web Standards
Knows HTML, XTML, CSS1, JavaScript, PHP, C++, DOM1
===
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top