www.webdeveloper.com
Results 1 to 4 of 4

Thread: Help with selected option

  1. #1
    Join Date
    Aug 2010
    Posts
    2

    Help with selected option

    hi! I just joined this form and hope to find and answer to my question. Consider me as a javascript newbie . My question is i have this for form with couple options and a text box and what I want to do is when user selects the "none" textbox to be enabled and if user chooses "mysite" option textbox to be disabled so user can`t put his/her input. here is my code:
    Code:
    <form id="form1" method="post" action="">
      <label>
        <select name="none" id="noneid" >
          <option value="none">None</option>
          <option value="mysite">Mysite</option>
        </select>
      </label>
      <label>
        Another
        <input type="text" name="another" id="anotherid" />
      </label>
    </form>
    I think i need a javascript function to trigger by onchange event to disable or enable textbox depending on the user input. But i dont know how to write that function.

    Thank You in advance for your help

  2. #2
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,460
    Hi there taocg,

    and a warm welcome to these forums.

    Try it like this...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title></title>
    
    <style type="text/css">
    .disabled {
        background-color:#d4d0c8;
     }
    </style>
    
    <script type="text/javascript">
    
    function disableEnable(){
    
       obj=document.getElementById('anotherid');
    
    document.getElementById('noneid').onchange=function() {
       this.value=='mysite'?
       (obj.disabled=true,obj.className='disabled'):
       (obj.disabled=false,obj.className='');
      }
     }
    
    if(window.addEventListener){
       window.addEventListener('load',disableEnable,false);
     }
    
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',disableEnable);
      }
     }
    </script>
    
    </head>
    <body>
    
    <form id="form1" method="post" action="#">
    <div>
      <label>
        <select name="none" id="noneid" >
          <option value="none">None</option>
          <option value="mysite">Mysite</option>
        </select>
      </label>
      <label>
        Another
        <input type="text" name="another" id="anotherid">
      </label>
    </div>
    </form>
    
    </body>
    </html>
    
    coothead

  3. #3
    Join Date
    Aug 2010
    Posts
    2
    Thank you for your help . This code solved my problem again Thank you very much.

  4. #4
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,460
    No problem, you're very welcome.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles