faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

172 of 209 people (82%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I disable a SELECT element?

May 2nd, 2000 07:22
Martin Honnen,


If you want to do it statically you need to use
  <SELECT ONFOCUS="this.blur()" ...>
for NN3/4 which don't support the HTML 4.0 DISABLED attribute and you 
can use
  <SELECT DISABLED ...>
for IE4+ and NN6.
To use both together:
  <SELECT DISABLED 
          ONFOCUS="if (!document.all && !document.getElementById)
                     this.blur();"
  >

If you want to dynamically disable/enable a SELECT use

<HTML>
<HEAD>
<SCRIPT>
function skip () { this.blur(); }
function toggleSelect (select) {
  if (!select.disabled) {
    select.disabled = true;
    if (!document.all && !document.getElementById) {
      select.oldOnFocus = 
        select.onfocus ? select.onfocus : null;
      select.onfocus = skip;
    }
  }
  else {
    select.disabled = false;
    if (!document.all && !document.getElementById) {
      select.onfocus = select.oldOnFocus;
    }
  }
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<SELECT NAME="aSelect">
<OPTION>Kibology
<OPTION>Xibology
</SELECT>
<INPUT TYPE="button" VALUE="toggle select"
       ONCLICK="toggleSelect(this.form.aSelect);"
>
</FORM>
</BODY>
</HTML>