Faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 20 people (55%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How do I populate a text box based on only certain values selected from a drop down box?

Apr 8th, 2008 18:54
ha mo, Dave Clark, gurudat prabhu,


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Gurudat,
     This may not be the answer you're looking for, however any other 
answer would probably require server-side code.  So...  This first 
solution is for when using a single-selection dropdown:
<form>
  <p><input type="text" name="mytext" size="40"><br>
  <select size="1" onchange="if(this.selectedIndex > 0)
   this.form.mytext.value+=this.options[this.selectedIndex].text+' ';
   return true;">
    <option>[select one]</option>
    <option>One</option>
    <option>Two</option>
    <option>Three</option>
    <option>Four</option>
    <option>Five</option>
    <option>Six</option>
  </select></p>
</form>
     This second solution is for when using a multiple-selection 
dropdown:
<form>
  <p><input type="text" name="mytext" size="40"><br>
  <select multiple size="6" onchange="this.form.mytext.value='';
   for (x=0; x<this.options.length; ++x)
     if (this.options[x].selected)
       this.form.mytext.value+=this.options[x].text+' ';
   return true;">
    <option>One</option>
    <option>Two</option>
    <option>Three</option>
    <option>Four</option>
    <option>Five</option>
    <option>Six</option>
  </select></p>
</form>
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com