Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting values from dynamically named input fields (ASP) 1

Status
Not open for further replies.

reisende

Programmer
Mar 16, 2004
74
US
Hi everybody.

I have a select field which is populated with a list of employees from a given location. The value of each option is set to the employee's assigned ID in the database.

What I'm trying to do is create an alert box which will activate when an employee is selected and display additional info about them (salaried, full time, etc.).

I want to be able to do this without cramming the option values with all the info separated by some delimiter. To do this I've created two hidden input fields which will hold the data I want to display.

Code:
<input type="hidden" name="<%= currentEmp %>Rate" value="<%= currentRate %>"></input>
<input type="hidden" name="<%= currentEmp %>Type" value="<%= currentPayType %>"></input>

I'm naming each of these fields dynamically through an ASP loop and setting their values as well.

I would like to retrieve the values in javascript and display them in the alert box using the dynamic field name.

Here's what I'm trying:

Code:
function showRate() {
  var f = document.forms.wageChange;
  var prempl = f.prempl.value;
  var fieldName = prempl+'Rate'
  var rate = f.fieldName.value;
  
  alert('Employee Rate: ' + rate);
}

The above function is called on an onChange() in the select list. All I get is a null value when I run it and I'm not much of a javascript guru to figure it out myself.

If anyone has any examples of how to do this or suggestions for a better way I am all ears.

Thanks.
 
Guessing premp1 is the name of your drop-down/select list.

Try adding the bold code below.

Code:
  var f = document.forms.wageChange;
  var prempl = f.prempl.value;
  var fieldName = prempl+'Rate'
  var rate = f.[b]elements[[/b]fieldName[b]][/b].value;

--Dave
 
As an explanation, you can't use a var in a statement like f.fieldName.value. In this case, the code is looking for a form element called fieldName. Forms have an array of objects called elements, however, and you can index them with a literal 'name' or a variable name.

'hope that helps.

--Dave
 
Welcome to the 100+ star club Dave.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
:) Thanks for noticing, kaht! I've been hovering just below for a long time and only just now found some time to hang out here again!

Good to "see" all the familiar "faces" still about!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top