Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8079773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:13:01+00:00 2026-06-05T16:13:01+00:00

I have a relatively simple HTML / JavaScript form. Such as this: <form method=’POST’

  • 0

I have a relatively simple HTML / JavaScript form. Such as this:

<form method='POST' action='someurl'>
    <label for='field1'>Your name</label>
    <input id='field1' name='field1' type='text' />

    <label for='field2'>Status</label>
    <select id='field2' name='field2'>
        <option>-select one-</option>
        <option value='Employee'>Employee</option>
        <option value='Retiree'>Retiree</option>
    </select>

    <label for='field3'>City you work in</label>
    <input id='field3' name='field3' type='text' />
</form>

What I need to be able to do, is change the text inside of the label for field3 based on the selected value in field2. So when field2 has ‘Employee’ selected the field3 label says ‘City you work in’, but when field2 has ‘Retiree’ selected then field3’s label should be ‘City where you live’.

One solution I have done is to have multiple tags inside of the field3 label, and then using some JavaScript I can toggle which of the tags inside the label is being displayed, like this:

// Form as above
<label for='field3'>
    <span id='field3EmployeeLabel' style='display:none;'>City you work in</span>
    <span id='field3RetireeLabel' style='display:none;'>City where you live</span>
</label>
<input id='field3' name='field3' type='text' />

function updateLabel() {
    if($('#field2').val()=='Employee') {
        $('#field3RetireeLabel').hide();
        $('#field3EmployeeLabel').show();
    } 
    else if($('#field2').val()=='Retiree') {
        $('#field3EmployeeLabel').hide();
        $('#field3RetireeLabel').show();
    }
    else {
        $('#field3RetireeLabel').hide();
        $('#field3EmployeeLabel').show();       
    }
}

The main reason that I don’t like this is that it all has to be coded by hand, and this is just a simple example, what if there are 7 or 8 different options in field2? Also, what if I had to combine multiple form field values to figure out which label to display?

Is there a design pattern or best-practice for this sort of thing in HTML? How have others handled this sort of requirement on their forms?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:13:03+00:00Added an answer on June 5, 2026 at 4:13 pm

    Create a map of values to labels to display http://jsfiddle.net/mendesjuan/VBVtE/3/ When you add new options, just add them to the labels map.

    JS

    $('#field2').change(function() {
        var labels = {
            'Employee' : 'City you work in',
            'Retiree' : 'City you live in',
            '-select one-': 'Default value'
        }      
        $("label[for='field3']").html(labels[$(this).val()]);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html form. This form has a specific width and margin
I have this relatively simple regex for usernames // Enforce that username has to
I have this simple hello world version of a google map api javascript v3
i have this simple code i just can't get it working. <html> <head> <script
Why is this simple HTML document not working right? What have I missed? I
I have a relatively simple application which I need to make native Mac OSX
I have a relatively simple query joining two tables. The Where criteria can be
I have a relatively simple question that I cannot seem to find the answer
I have a relatively simple subset of tables in my database for tracking something
I have a relatively simple class. I serialize it using JDK 1.7 on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.