Results 1 to 3 of 3

Thread: Enabling a text field using a <SELECT> input

  1. #1
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Enabling a text field using a <SELECT> input

    I'm trying to enable a disabled text field based on a specific value being selected in a <SELECT> box. Here's the code I'm using:

    // JavaScript Document
    function showhideYearsInAlaska(obj) {
    txt = document.getElementById('residency').options.selectedIndex.value;
    if ( txt == 'Alaska' ) {
    document.getElementById('years_in_alaska').disabled = false;
    }
    }

    The code for calling this function from the <SELECT> box is as follows:

    <p>
    <label for="residency">In what state do you claim official residency?</label>
    <select name="residency" id="residency" tabindex="18" onchange="showhideYearsInAlaska(this)">
    <option value="select">Select a State</option>
    <option value="Alaska" id="Alaska">Alaska</option>
    </select>
    </p>
    <p>
    <label for="years_in_alaska">If you claim Alaska residency, how long have you been physically in Alaska?</label>
    <input type="text" name="years_in_alaska" id="years_in_alaska" disabled="true" />
    </p>

    However, it's not working...and I don't understand why not.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You could get sort of what I think you want via this tortured path:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function showhideYearsInAlaska(obj) {
    var txt = document.getElementById('residency').options[document.getElementById('residency').options.selectedIndex].value;
    if ( txt == 'Alaska' ) {
    document.getElementById('years_in_alaska').disabled = false;
    }
    }
    </script>
    </head>
    <body>
    <p>
    <label for="residency">In what state do you claim official residency?</label>
    <select name="residency" id="residency" tabindex="18" onchange="showhideYearsInAlaska(this)">
    <option value="select">Select a State</option>
    <option value="Alaska" id="Alaska">Alaska</option>
    </select>
    </p>
    <p>
    <label for="years_in_alaska">If you claim Alaska residency, how long have you been physically in Alaska?</label>
    <input type="text" name="years_in_alaska" id="years_in_alaska" disabled="true" />
    </p>
    </body>
    </html>
    Note: Both the obj argument and the this parameter passed to it are unused.

    Here is a much simpler method that also allows for the field to become disabled again should the user change their selection:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function showhideYearsInAlaska(val) {
    document.getElementById('years_in_alaska').disabled = val !== 'Alaska';
    }
    </script>
    </head>
    <body>
    <p>
    <label for="residency">In what state do you claim official residency?</label>
    <select name="residency" id="residency" tabindex="18" onchange="showhideYearsInAlaska(this.value)">
    <option value="select">Select a State</option>
    <option value="Alaska" id="Alaska">Alaska</option>
    </select>
    </p>
    <p>
    <label for="years_in_alaska">If you claim Alaska residency, how long have you been physically in Alaska?</label>
    <input type="text" name="years_in_alaska" id="years_in_alaska" disabled="true" />
    </p>
    </body>
    </html>
    Now, this just deals with the field becoming enabled or disabled though, which is different from hiding and showing the field. I think you are just going for enabled/disabled. But if you want more, be specific.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thank you

    Thank you jscheuer1. That worked very well indeed.

Bookmarks

Posting Permissions

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