Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow Update Selected Option Based on another Entry

    I have an options list that is generated from a database. It currently has ~125 options in it.

    I've received the request that the user be able to either enter the corresponding number or select the option from the list.

    How can I update the "selected" attribute in the options list based on a number that may be entered in another text entry box?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    like this, kinda?

    Code:
    <input type="button" value="select number" onclick="changeList()">
    <input type="text" id="tb">
    
    <select id="select" onchange="" >
      <option> Select </option> 
      <option value="1"> one </option> 
      <option value="2"> two </option>
      <option value="3"> three </option> 
    </select>
    <script type="text/javascript">
    function changeList() {
    document.getElementById('select').selectedIndex=document.getElementById('tb').value;
    }
    </script>

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    ... remember you can also group options to put common choices at the top (apologies for code dump - it seems there are rather a lot of countries in the world)

    Code:
    <!DOCTYPE html>
    <html>
      <head>
      <title>blank</title>
        <meta charset="UTF-8">
    </head>
    <body>
    <input type="button" value="select number" onclick="changeList()">
    <input type="text" id="tb">
    <select id="select">
    	<optgroup label="">
    		<option value="" selected="selected">Select Country</option> 
    	</optgroup>
    	<optgroup label="common choices">
    <option value="236">United Kingdom</option>
    <option value="237">United States</option>
    <option value="77">France</option>
    <option value="85">Germany</option>
    <option value="40">Canada</option>
    <option value="209">Spain</option>
    	</optgroup>
    	<optgroup label="other countries">
    <option value="1">Afghanistan</option>
    <option value="2">Albania</option>
    <option value="3">Algeria</option>
    <option value="4">American Samoa</option>
    <option value="5">Andorra</option>
    <option value="6">Angola</option>
    <option value="7">Anguilla</option>
    <option value="8">Antarctica</option>
    <option value="9">Antigua and Barbuda</option>
    <option value="10">Argentina</option>
    <option value="11">Armenia</option>
    <option value="12">Armenia</option>
    <option value="13">Aruba</option>
    <option value="14">Australia</option>
    <option value="15">Austria</option>
    <option value="16">Azerbaijan</option>
    <option value="17">Azerbaijan</option>
    <option value="18">Bahamas</option>
    <option value="19">Bahrain</option>
    <option value="20">Bangladesh</option>
    <option value="21">Barbados</option>
    <option value="22">Belarus</option>
    <option value="23">Belgium</option>
    <option value="24">Belize</option>
    <option value="25">Benin</option>
    <option value="26">Bermuda</option>
    <option value="27">Bhutan</option>
    <option value="28">Bolivia</option>
    <option value="29">Bosnia and Herzegovina</option>
    <option value="30">Botswana</option>
    <option value="31">Bouvet Island</option>
    <option value="32">Brazil</option>
    <option value="33">British Indian Ocean Territory</option>
    <option value="34">Brunei Darussalam</option>
    <option value="35">Bulgaria</option>
    <option value="36">Burkina Faso</option>
    <option value="37">Burundi</option>
    <option value="38">Cambodia</option>
    <option value="39">Cameroon</option>
    <option value="41">Cape Verde</option>
    <option value="42">Cayman Islands</option>
    <option value="43">Central African Republic</option>
    <option value="44">Chad</option>
    <option value="45">Chile</option>
    <option value="46">China</option>
    <option value="47">Christmas Island</option>
    <option value="48">Cocos (Keeling) Islands</option>
    <option value="49">Colombia</option>
    <option value="50">Comoros</option>
    <option value="51">Congo</option>
    <option value="52">Congo, The Democratic Republic of The</option>
    <option value="53">Cook Islands</option>
    <option value="54">Costa Rica</option>
    <option value="55">Cote D'ivoire</option>
    <option value="56">Croatia</option>
    <option value="57">Cuba</option>
    <option value="58">Cyprus</option>
    <option value="59">Cyprus</option>
    <option value="60">Czech Republic</option>
    <option value="61">Denmark</option>
    <option value="62">Djibouti</option>
    <option value="63">Dominica</option>
    <option value="64">Dominican Republic</option>
    <option value="65">Easter Island</option>
    <option value="66">Ecuador</option>
    <option value="67">Egypt</option>
    <option value="68">El Salvador</option>
    <option value="69">Equatorial Guinea</option>
    <option value="70">Eritrea</option>
    <option value="71">Estonia</option>
    <option value="72">Ethiopia</option>
    <option value="73">Falkland Islands (Malvinas)</option>
    <option value="74">Faroe Islands</option>
    <option value="75">Fiji</option>
    <option value="76">Finland</option>
    <option value="78">French Guiana</option>
    <option value="79">French Polynesia</option>
    <option value="80">French Southern Territories</option>
    <option value="81">Gabon</option>
    <option value="82">Gambia</option>
    <option value="83">Georgia</option>
    <option value="84">Georgia</option>
    <option value="86">Ghana</option>
    <option value="87">Gibraltar</option>
    <option value="88">Greece</option>
    <option value="89">Greenland</option>
    <option value="90">Greenland</option>
    <option value="91">Grenada</option>
    <option value="92">Guadeloupe</option>
    <option value="93">Guam</option>
    <option value="94">Guatemala</option>
    <option value="95">Guinea</option>
    <option value="96">Guinea-bissau</option>
    <option value="97">Guyana</option>
    <option value="98">Haiti</option>
    <option value="99">Heard Island and Mcdonald Islands</option>
    <option value="100">Honduras</option>
    <option value="101">Hong Kong</option>
    <option value="102">Hungary</option>
    <option value="103">Iceland</option>
    <option value="104">India</option>
    <option value="105">Indonesia</option>
    <option value="106">Indonesia</option>
    <option value="107">Iran</option>
    <option value="108">Iraq</option>
    <option value="109">Ireland</option>
    <option value="110">Israel</option>
    <option value="111">Italy</option>
    <option value="112">Jamaica</option>
    <option value="113">Japan</option>
    <option value="114">Jordan</option>
    <option value="115">Kazakhstan</option>
    <option value="116">Kazakhstan</option>
    <option value="117">Kenya</option>
    <option value="118">Kiribati</option>
    <option value="119">Korea, North</option>
    <option value="120">Korea, South</option>
    <option value="121">Kosovo</option>
    <option value="122">Kuwait</option>
    <option value="123">Kyrgyzstan</option>
    <option value="124">Laos</option>
    <option value="125">Latvia</option>
    <option value="126">Lebanon</option>
    <option value="127">Lesotho</option>
    <option value="128">Liberia</option>
    <option value="129">Libyan Arab Jamahiriya</option>
    <option value="130">Liechtenstein</option>
    <option value="131">Lithuania</option>
    <option value="132">Luxembourg</option>
    <option value="133">Macau</option>
    <option value="134">Macedonia</option>
    <option value="135">Madagascar</option>
    <option value="136">Malawi</option>
    <option value="137">Malaysia</option>
    <option value="138">Maldives</option>
    <option value="139">Mali</option>
    <option value="140">Malta</option>
    <option value="141">Marshall Islands</option>
    <option value="142">Martinique</option>
    <option value="143">Mauritania</option>
    <option value="144">Mauritius</option>
    <option value="145">Mayotte</option>
    <option value="146">Mexico</option>
    <option value="147">Micronesia, Federated States of</option>
    <option value="148">Moldova, Republic of</option>
    <option value="149">Monaco</option>
    <option value="150">Mongolia</option>
    <option value="151">Montenegro</option>
    <option value="152">Montserrat</option>
    <option value="153">Morocco</option>
    <option value="154">Mozambique</option>
    <option value="155">Myanmar</option>
    <option value="156">Namibia</option>
    <option value="157">Nauru</option>
    <option value="158">Nepal</option>
    <option value="159">Netherlands</option>
    <option value="160">Netherlands Antilles</option>
    <option value="161">New Caledonia</option>
    <option value="162">New Zealand</option>
    <option value="163">Nicaragua</option>
    <option value="164">Niger</option>
    <option value="165">Nigeria</option>
    <option value="166">Niue</option>
    <option value="167">Norfolk Island</option>
    <option value="168">Northern Mariana Islands</option>
    <option value="169">Norway</option>
    <option value="170">Oman</option>
    <option value="171">Pakistan</option>
    <option value="172">Palau</option>
    <option value="173">Palestinian Territory</option>
    <option value="174">Panama</option>
    <option value="175">Papua New Guinea</option>
    <option value="176">Paraguay</option>
    <option value="177">Peru</option>
    <option value="178">Philippines</option>
    <option value="179">Pitcairn</option>
    <option value="180">Poland</option>
    <option value="181">Portugal</option>
    <option value="182">Puerto Rico</option>
    <option value="183">Qatar</option>
    <option value="184">Reunion</option>
    <option value="185">Romania</option>
    <option value="186">Russia</option>
    <option value="187">Russia</option>
    <option value="188">Rwanda</option>
    <option value="189">Saint Helena</option>
    <option value="190">Saint Kitts and Nevis</option>
    <option value="191">Saint Lucia</option>
    <option value="192">Saint Pierre and Miquelon</option>
    <option value="193">Saint Vincent and The Grenadines</option>
    <option value="194">Samoa</option>
    <option value="195">San Marino</option>
    <option value="196">Sao Tome and Principe</option>
    <option value="197">Saudi Arabia</option>
    <option value="198">Senegal</option>
    <option value="199">Serbia and Montenegro</option>
    <option value="200">Seychelles</option>
    <option value="201">Sierra Leone</option>
    <option value="202">Singapore</option>
    <option value="203">Slovakia</option>
    <option value="204">Slovenia</option>
    <option value="205">Solomon Islands</option>
    <option value="206">Somalia</option>
    <option value="207">South Africa</option>
    <option value="208">South Georgia and The South Sandwich Islands</option>
    <option value="210">Sri Lanka</option>
    <option value="211">Sudan</option>
    <option value="212">Suriname</option>
    <option value="213">Svalbard and Jan Mayen</option>
    <option value="214">Swaziland</option>
    <option value="215">Sweden</option>
    <option value="216">Switzerland</option>
    <option value="217">Syria</option>
    <option value="218">Taiwan</option>
    <option value="219">Tajikistan</option>
    <option value="220">Tanzania, United Republic of</option>
    <option value="221">Thailand</option>
    <option value="222">Timor-leste</option>
    <option value="223">Togo</option>
    <option value="224">Tokelau</option>
    <option value="225">Tonga</option>
    <option value="226">Trinidad and Tobago</option>
    <option value="227">Tunisia</option>
    <option value="228">Turkey</option>
    <option value="229">Turkey</option>
    <option value="230">Turkmenistan</option>
    <option value="231">Turks and Caicos Islands</option>
    <option value="232">Tuvalu</option>
    <option value="233">Uganda</option>
    <option value="234">Ukraine</option>
    <option value="235">United Arab Emirates</option>
    <option value="238">United States Minor Outlying Islands</option>
    <option value="239">Uruguay</option>
    <option value="240">Uzbekistan</option>
    <option value="241">Vanuatu</option>
    <option value="242">Vatican City</option>
    <option value="243">Venezuela</option>
    <option value="244">Vietnam</option>
    <option value="245">Virgin Islands, British</option>
    <option value="246">Virgin Islands, U.S.</option>
    <option value="247">Wallis and Futuna</option>
    <option value="248">Western Sahara</option>
    <option value="249">Yemen</option>
    <option value="250">Yemen</option>
    <option value="251">Zambia</option>
    <option value="252">Zimbabwe</option>
    </optgroup>
    </select>
    <script type="text/javascript">
    function changeList() {
    document.getElementById('select').selectedIndex=document.getElementById('tb').value;
    }
    </script>
    </body>
    </html>

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I haven't tried it yet, but this looks much easier than I had envisioned - but it looks like this would be a good option.

    Thanks!

    Quote Originally Posted by xelawho View Post
    like this, kinda?

    Code:
    <input type="button" value="select number" onclick="changeList()">
    <input type="text" id="tb">
    
    <select id="select" onchange="" >
      <option> Select </option> 
      <option value="1"> one </option> 
      <option value="2"> two </option>
      <option value="3"> three </option> 
    </select>
    <script type="text/javascript">
    function changeList() {
    document.getElementById('select').selectedIndex=document.getElementById('tb').value;
    }
    </script>

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    there's just one thing to note if you're going with the values-as-numbers approach:

    selectedIndex gives you the option's position on the list, so in the second example, entering 3 brings up France (because it's third on the list) even though its value is 77

    if you want to change the selected option according to the value then that line in the function would be
    Code:
    document.getElementById('select').value=document.getElementById('tb').value;
    that way typing in 3 brings up Algeria

  • Users who have thanked xelawho for this post:

    mkswanson (10-01-2011)


  •  

    Posting Permissions

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