Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Set FORM field value based on value of another field

Posted on 2001-08-28
8
Medium Priority
?
318 Views
Last Modified: 2012-05-04
Here's what I need:

A small javascrip application, as horizontally-compatible as possible (with any details of browsers/situations where it may not be compatible and why) that will set the value of one field in a FORM based on the value of another field.  And a variation that can set the value of one field to equal the value of another field plus some additional text.

I want to know all the ways this script can be executed, i.e. do I have to put a reference in the FORM or submit button or is there a way to have it function automatically by embedding the script at different points in the html page?

Example:

<form method="post" action="...">
<select name="fieldA">
<option value="a">First Option
<option value="2">Second Option
..
</select>
... (more stuff)

I'd like examples of ways a hidden field, say like:
<input type="hidden" name="fieldAdesc" value="..">

can be set to:
  "First Option" if fieldA="a"
  "Second Option" if fieldA="2"
  etc.

and how in which this can be done... only upon submit, in real time to change the field value at the time of selecting the pulldown - etc?  Ideally, it would be nice to have the script set the field value when the pulldown is selected - and can this be done without changing the FORM or SUBMIT references?

Thanks in advance for your assistance.  Extra good kharma will also be awarded to the person with the most complete answer!
0
Comment
Question by:maabu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Accepted Solution

by:
de_britto earned 200 total points
ID: 6433995
<Script language="JavaScript">
fnSetCombo() {
document.fm.fieldAdesc.value = document.fm.fieldA.options[document.fm.fieldA.selectedIndex].text;
}
</Script>


Call this function 'onChange' of select control
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6434071
Or neater (adding a "function" too) :

<Script language="JavaScript">
function fnSetCombo(theSel) {
theSel.form.fieldAdesc.value = theSel.options[theSel.selectedIndex].text;
}
</Script>

<select name="fieldA" onChange="fnSetCombo(this)">

or all in the onChange - no need for script tags:

<select name="fieldA"
onChange="this.form.fieldAdesc.value=this.options[this.selectedIndex].text">
0
 
LVL 1

Author Comment

by:maabu
ID: 6434104
I know nothing of Javascript so "dummy-fy" this for me by showing me how this would fit into the sample form if you fine gentlemen wouldn't mind.   I can't tell what phrases are variables and what are reserved words either.. can you make the distinction?

Thanx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6434128
<form method="post" action="...">

<select name="fieldA"
onChange="this.form.fieldAdesc.value=this.options[this.selectedIndex].text">

<option value="a">First Option
<option value="2">Second Option
</select>
<input type="hidden" name="fieldAdesc" value="">

Each field has a "form"
that points at the form it is in
so this.form
is the select's form
this.form.fieldAdesc
is the hidden field as seen from the select
the select has options: this.options
they are accessed via the selectedIndex e.g. 0 for the first option, 1 for the second (javascript counts from 0)

the notation
this.options[this.selectedIndex]
is the selected option.

It has a .value and a .texts, the latter being what you asked for

It is trigger when the user changes the select e.g. onChange
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6434136
Oops - typo:
It has a .value and a .texts,
should be
It has a .value and a .text,

Michel
0
 
LVL 12

Expert Comment

by:ahosang
ID: 7962300
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
points to mplungjan
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 7964754
points to me or split me and deBritto
0
 

Expert Comment

by:SpideyMod
ID: 8005894
Points split

SpideyMod
Community Support Moderator @Experts Exchange

mplungjan points for you at:
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20526348.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question