Enable/Disable Drop Down on Selection HELP..

Discussion in 'JavaScript' started by Dangy, Jan 27, 2009.

  1. #1
    I personally have no javascript experience what so ever. I'm just looking for someone who can write the code for me for free.

    Example:


    <form id="form1" name="form1" method="post" action="">
    
    <option value="US" selected="selected">United States</option>
    <option value="AU">Australia</option>
    <option value="CA">Canada</option>
    <option value="GB">United Kingdom</option>
    <option value="B"></option>
    
    <option value="AF">Afghanistan</option>
    <option value="AL">Albania</option>
    <option value="DZ">Algeria</option>
    
    <select name="states" class="select">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    
    <option value="AR">Arkansas</option>
    <option value="AA">Armed Forces Americas</option>
    <option value="AE">Armed Forces Europe</option>
    <option value="AP">Armed Forces Pacific</option>
    <option value="CA">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
    <option value="DE">Delaware</option>
    
    </form>
    
    HTML:
    I want the "states" drop down disabled unless the value "US" is selected on the "country" drop down.
     
    Dangy, Jan 27, 2009 IP
  2. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    this should work, though it wasn't tested:

    
    <script type="text/javascript">
    function enableStates(to_enable)
    {
      var statesSelectObj = document.getElementById('states_select');
      if (statesSelectObj)
      {
        if (to_enable)
        {
          statesSelectObj.removeAttribute('disabled');
        }
        else
        {
          statesSelectObj.setAttribute('disabled', 'disabled');
        }
      }
    }
    </script>
    <form id="form1" name="form1" method="post" action="">
    
    <!-- you forgot to put this select tag... -->
    <select name="country_select" onchange="enableStates(this.options[this.selectedIndex].value == 'US');">[/B]
    
    <option value="US" selected="selected">United States</option>
    <option value="AU">Australia</option>
    <option value="CA">Canada</option>
    <option value="GB">United Kingdom</option>
    <option value="B"></option>
    
    <option value="AF">Afghanistan</option>
    <option value="AL">Albania</option>
    <option value="DZ">Algeria</option>
    
    <!-- and end select tag -->
    </select>
    
    <select name="states" class="select" id="states_select">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    
    <option value="AR">Arkansas</option>
    <option value="AA">Armed Forces Americas</option>
    <option value="AE">Armed Forces Europe</option>
    <option value="AP">Armed Forces Pacific</option>
    <option value="CA">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
    <option value="DE">Delaware</option>
    
    </form>
    
    HTML:
     
    yoavmatchulsky, Jan 28, 2009 IP
  3. Dangy

    Dangy Well-Known Member

    Messages:
    841
    Likes Received:
    25
    Best Answers:
    2
    Trophy Points:
    155
    #3
    Work's perfect I thank you.
     
    Dangy, Jan 28, 2009 IP