disable/enable textbox

kp_y
kp_y used Ask the Experts™
on
i have a drop-down box and a textbox
how do i disable the textbox by default, then if user select certain value in the textbox, the textbox will be enabled to allow user to enter value.  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The following example will help you. say u have three options inyour drop-down and on selecting the second item it will enable the text box. Try and see!

<HTML>
<BODY onload="DisableTextbox()">
     <select name="cboTest" onchange="EnableTextbox()">
          <option value='1'>one
          <option value='2'>Two
          <option value='3'>Three
     </select>
     <input type="text" name="txtTest">
</BODY>
<script language="javascript">
     function DisableTextbox()
     {
          txtTest.disabled = true;
     }
     
     function EnableTextbox()
     {
          if (cboTest.value == '2')
               txtTest.disabled = false;
          else
          {
               txtTest.value = '';
               txtTest.disabled = true;
          }
     }
</script>
</HTML>
NOTE: disabled controls will not be posted in ASP ...

minimize the javascript and disable the textbox right in the HTML:

<input type=text id=txtMyText DISABLED>
<select id=cboMySelect onChange="javascript:enableTextBox();">
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
</select>

<script language=javascript>
<!--
  function enableTextBox()
  {
    var idx = document.forms[0].cboMySelect.selectedIndex;

    //any old selected value
    if (idx != -1)
    {
      document.forms[0].txtMyText.disabled = false;
    }

    // or for a specific value
    if (document.forms[0].cboMySelect.options[idx].value == "2")
    {
      document.forms[0].txtMyText.disabled = false;
    }
  }
//-->
</script>

to alleviate cross-browser issues, try coding according to the DOM -- saves headaches

thomasdodds
Top Expert 2013

Commented:
This question has been abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.
<note>
   In the absence of responses, I may recommend DELETE unless it is clear
   to me that it has value as a PAQ.  Silence = you don't care
</note>

Cd&
Top Expert 2013

Commented:
It is time to clean this abandoned question up.

I am putting it on a clean up list for CS.

<recommendation>
points to son_robin

</recommendation>

If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.

Cd&

per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial