Results 1 to 3 of 3

Thread: automatically populate a hidden field based on a drop-down selection

  1. #1
    Join Date
    Jun 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default automatically populate a hidden field based on a drop-down selection

    I have a form with a drop-down for Title (Mr, Mrs, Miss, Ms)
    I'd like the value of a hidden field for Gender to be automatically updated according to the title selected.

    So I need:

    If Title.value = Mr
    Then
    Gender.value = Male
    Else
    Gender.value = Female

    Except I can't get the syntax right, could someone give me a hand?

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,793
    Thanks
    2
    Thanked 418 Times in 412 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    
    function Gender(sel,nme){
     var txt=sel.options[sel.selectedIndex].text;
     sel.form[nme].value=txt.indexOf('M')==0?txt.indexOf('s')>0?'Female':'Male':'';
    }
    
    </script></head>
    
    <body>
    <form>
    <select onchange="Gender(this,'gender');" >
    <option >Title</option>
    <option >Mr</option>
    <option >Mrs</option>
    <option >Ms</option>
    <option >Miss</option>
    </select>
    <input name="gender" />
    </form>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jun 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks vwphillips!

    Not how I thought at all but that did the job. Fantastic, thank you

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
  •