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

  • SEARCH
  • Home
  • 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 7563569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:41:54+00:00 2026-05-30T13:41:54+00:00

I can’t quite get my head around this idea, wonder if someone could help

  • 0

I can’t quite get my head around this idea, wonder if someone could help out please?

I have a form with 12 options, option ‘1’ is selected by default. There is a hidden field that is populated with corresponding values.

What I want to achieve is: When option 1 is selected, the value of the hidden text field updates to “Opt1”, and so on through to “Opt12”

I hope the following renders correctly as XHTML

The best way for me to explain if I may, is as follows

    <select name="somethingName" id="SomethingID">
<option value="1" selected="selected">Option #1</option>
<option value="2ABC">Option #2</option>
<option value="3ABC">Option #3</option>
<option value="4ABC">Option #4</option>
<option value="5ABC">Option #5</option>
<option value="6ABC">Option #6</option>
<option value="7ABC">Option #7</option>
<option value="8ABC">Option #8</option>
<option value="9ABC">Option #9</option>
<option value="10ABC">Option #10</option>
<option value="11ABC">Option #11</option>
<option value="12ABC">Option #12</option>
</select>

<input type='hidden' id='myhidden' value=''>

If/When Option #1 is selected, the value of myHidden is equal to “Opt1”
If/When Option #2 is selected, the value of myHidden is equal to “Opt2”

and so on. I say if/when for the idea that the hidden text field is updated on select.

Can anyone please suggest how I would achieve this with Jquery?

  • 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-05-30T13:41:55+00:00Added an answer on May 30, 2026 at 1:41 pm

    Here’s how you can do it:

    1. Bind the change event of the select element using .change(handler). “handler” is a function that will be called when a change event is fired.

    2. Get the selected option element using the :selected selector.

    3. when using .index() without parameter, it returns the index of the matched element relatively to its siblings. So as you get the option element that is selected, it will return its index relatively to the other option elements. This implies of course your options are sorted. the returned index is zero-based.

    4. Set the value of the hidden field with .val().

    5. To directly apply the change when an option is preselected, trigger the change event with .change() (no parameters).

    Here’s the code:

    var $select = $('#SomethingID'),
        $hidden = $('#myhidden');
    
    $select.change(function(e) {
        var idx = $select.find('option:selected').index() + 1;
        $hidden.va('Opt' + idx);
    }).change();​
    

    DEMO


    So if the actual value to set in the hidden field is not based on the index of the option element, but a random value, the best choice is to use data attribute. This way you can store the corresponding value to set on each option element:

    <select name="somethingName" id="SomethingID">
        <option value="1" data-val="QBQ" selected="selected">Option #1</option>
        <option value="2ABC" data-val="ALA">Option #2</option>
        <option value="3ABC" data-val="OLP">Option #3</option>
    </select>
    

    To get the value of the data attribute, use the .data() method:

    var $select = $('#SomethingID'),
        $hidden = $('#myhidden');
    
    $select.change(function(e) {
        var val = $select.find('option:selected').data('val');
        $hidden.va(val);
    }).change();​
    

    DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
Can someone maybe tell me why this is not working? I have used echo
This could be a duplicate question, but I have no idea what search terms
Can someone please explain how this code is working for me? I don't understand
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone help me trying to find out why this doesn't work. The brushes
Can someone please explain to me why the output from the following code is
can someone explain what means this line, or provide with source where it is
Can someone please explain what GZIP Output Compression Level is and how it affects
Can someone help me with a command line instruction to drop a single MAMP

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.