AllExperts > Experts 
Search      

Javascript

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Javascript Answers
Question Library

Ask a question about Javascript
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Anthony Levensalor
Experience
I have been programming in Java and Javascript since 1998. I run a web development company that specializes in Ajax front ends with PHP/MySQL back ends. I am a Sun Certified Java 2 Developer, and have done nothing but web applications for the last two years in my business

Past/Present clients
Monster.com, Compuware Corporation, Flextronics International, Pragmatech Software, Open Travel Software, The Loss Mitigation Institute, Raw Story Media, Page One News Media.

 
   

You are here:  Experts > Computing/Technology > Focus on JavaScript > Javascript > Force a ListBox Selection From another Field

Topic: Javascript



Expert: Anthony Levensalor
Date: 1/14/2008
Subject: Force a ListBox Selection From another Field

Question
This is a Acrobat Form.

Is there a way to force a selection in a ListBox from another field?

If radio button 1 is selected then the contents of text box 1 is the value we want selected in the ListBox.
If radio button 2 is selected then the contents of text box 2 is the value we want selected in the ListBox.
If radio button 3 is selected then the contents of text box 3 is the value we want selected in the ListBox.

The contents of the text boxes are values that are in the ListBox data.

Both the ListBox and the text boxes are being populated from external databases.

Any and all advice is welcome.

Answer
Yes, there absolutely is, and it's actually pretty easy once you get it the first time.

Given
3 input elements of type radio
3 input elements of type text
1 select element

We could write the following code that would select the value in the select element when the radio buttons were clicked to the select element matching the text:

(Comments are in the source below, which you can copy directly to an empty file to see it work)

<html>
 <head>
   <script type="text/javascript">
     // This function takes an event as its only parameter    
     function updateSelect(e) {
       // Here, we use object detection to make sure it will work across browsers
       e = e || window.event
       
       // Here, we get the element that fired the event.
       // Again, we use object detection to make sure it works
       // across multiple environments
       var elm = e.target || e.srcElement
       
       // In the HTML, we have used a simple naming scheme to
       // match radios to their text field. Here,
       // we use that naming scheme
       var txtElmId = elm.id.replace("radio", "txt")        
       
       // And then grab the right fields value
       var textValue = document.getElementById(txtElmId).value
       
       // Here we just get the reference to the select element
       var selectElm = document.getElementById('main_list')
       
       // oLen will hold the nunber of options in the list
       var oLen = selectElm.options.length
       
       // Here we loop through the options, matching the text of
       // the text input to the select input options
       for (var i = 0; i < oLen; ++i) {
         if (selectElm.options[i].text == textValue) {
           selectElm.value = selectElm.options[i].value
           break;
         }
       }
       
       
     } // updateSelect
     
     // This function sets the even handlers.
     // It fires on the load event of the main body  
     function init() {
       if (document.getElementById("radioThree")) {
         document.getElementById("radioOne").onclick = updateSelect
         document.getElementById("radioTwo").onclick = updateSelect
         document.getElementById("radioThree").onclick = updateSelect
       } else {
         // We're using setTimeout here to make sure the elements
         // are avilable for use. If this function were to fire
         // before the elements fully load, it will wait and re-fire
         // in ten milliseconds. It will keep doing this until the
         // elements we want are usable
         setTimeout(init, 10)
       }                
     }
     
   </script>
 </head>
 <body onload="init()">    
   <input type="radio" name="choice" id="radioOne"><input type="text" id="txtOne" value="Anthony">
   <input type="radio" name="choice" id="radioTwo"><input type="text" id="txtTwo" value="Michael">
   <input type="radio" name="choice" id="radioThree"><input type="text" id="txtThree" value="Chris">
  
   <select size="1" id="main_list">
     <option value="0">Anthony</option>
      <option value="1">Michael</option>
      <option value="2">Chris</option>
    </select>
</select>
 </body>
</html>

Hope this helps,
~A!  

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.