473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Dynamically assign value to hidden fields?

Hello,
I have a function that among other things, assigns a value to a hidden
field element. However, I want to pass the name of a hidden field to
my function and then assign it a value. So instead of this:

document.forms[0].elements[4].value = "this is a test";

I want to be able to do something like this:

document.forms[0].+MyVarName+.value = "this is a test";

Where MyVarName is the name of the field at position 4 in the array of
form elements.

Does anyone know if this is possible? When I try to do this I
generate an error. Thanks for any help!

....Brad
Jul 20 '05 #1
2 24464
You can do that using eval() function, e.g.

function assignValue(MyVarName){
str = "document.forms[0]."+MyVarName+".value = 'this is a test'";
eval(str);
}

but you should consider using DOM, something like this

document.getElementById(MyVarName).value = "test"
"Brad Melendy" <br**@melendy.com> wrote in message
news:c5*************************@posting.google.co m...
Hello,
I have a function that among other things, assigns a value to a hidden
field element. However, I want to pass the name of a hidden field to
my function and then assign it a value. So instead of this:

document.forms[0].elements[4].value = "this is a test";

I want to be able to do something like this:

document.forms[0].+MyVarName+.value = "this is a test";

Where MyVarName is the name of the field at position 4 in the array of
form elements.

Does anyone know if this is possible? When I try to do this I
generate an error. Thanks for any help!

...Brad

Jul 20 '05 #2
"Vjekoslav Begovic" <vj*******@inet.hr> writes:
You can do that using eval() function, e.g.

function assignValue(MyVarName){
str = "document.forms[0]."+MyVarName+".value = 'this is a test'";
eval(str);
}
You *never* need eval to access properties. This can be written
without eval as:
document.forms[0][MyVarName].value = "this is a test";
or even better:
document.forms[0].elements[MyVarName].value = "this is a test";
but you should consider using DOM, something like this

document.getElementById(MyVarName).value = "test"


"document.forms" is just as legal DOM as "document.getElementById".

/L 'please don't top post'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lukelrc | last post by:
Hi, I have a dynamically created listbox. I'm trying to get one of the options selected according to a passed value. This is what i have: <select name="txtTheme" id="txtTheme"> ...
4
by: Terry | last post by:
I have a number of input boxes used to display totals based on selected items for each row in a table. There are more than a few rows that are identical, except for the form field name. I have...
2
by: juglesh | last post by:
hi, all, thanks for reading. i have a form in which i want drop down boxes to dynamically change some hidden fields: http://cynthialoganjewelry.com/test4.htm <form name=test method="post" > ...
5
by: Good Man | last post by:
Hi there I'm adding form fields on the fly with some javascript DOM programming. I basically just clone a hidden <div>, then adjust node properties to make this new <div> have unique values...
2
by: Megan | last post by:
Hi everybody- I have 2 tables, Hearings and Rulings. Both have primary keys called, CaseID, that are autonumbers. I don't want both tables to have the same autonumber. For example, if Hearings...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
1
by: vega80 | last post by:
Hi. I have a problem with assigning an onkeypress-function to dynamically created input-boxes.I want to put the content of an input-field into a tag-list when the user hits enter. This works...
7
by: Srikanth Ram | last post by:
Hi, I'm creating a PHP application. In this a dynamic table with the fields in the database is generated in a page. I have placed a checkbox in each row of the table to approve/disapprove...
1
by: Louis | last post by:
I am building a JS library (to be used in an HTML page, of course) to get and process some data via Google Maps API. The whole result is stored in a single JS object. Then I want to upload them to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.