473,788 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with document.formna me.variable.val ue

I need to write a function that copies variables to fields. I've used an
array and loop because it's neater than writing a similar sentence out
10 times.

var myString = new
String("id,call ername,address, phone,phoneb,em ail,sqft,source ,action,status" );
var myFields = myString.split( ',');

//myValues is an array of values passed to the function
var myValues =
[id,callername,a ddress,phone,ph oneb,email,sqft ,source,action, status];

for(var i=0;i<myFields. length;i++) {
document.formna me.myFields[i].value=myValues[i];
}

Firefox returns 'document.edit. myFields' has no properties. IE doesnt
like it either. I'm sure this is a simple question but Google has turned
up nothing :(
Jul 23 '05 #1
2 2810
Scott wrote:
I need to write a function that copies variables to fields. I've used an
array and loop because it's neater than writing a similar sentence out
10 times.

var myString = new
String("id,call ername,address, phone,phoneb,em ail,sqft,source ,action,status" );

var myFields = myString.split( ',');

It seems a bit pointless to create a string purely for the sake
of then creating an array. Why not just create an array?

var myFields = [
'id','callernam e','address','p hone',
'phoneb','email ','sqft','sourc e',
'action','statu s'
];

//myValues is an array of values passed to the function
var myValues =
[id,callername,a ddress,phone,ph oneb,email,sqft ,source,action, status];
whatever was passed has now obliterated by re-initialising the
variable. It isn't good to have an input element called 'id',
it will be easily confused with the id attribute and may cause
issues.

You should have something like:
function theFuntion() {
....

And call it with (presuming the values are passed as strings):

theFunction('an Id','aCallernam e','anAddress', 'aPhone',
'aPhoneB','anEm ail','aSqft','t heSource',
'theAction','th eStatus');
...

now use the arguments collection to get at the values passed to
the function. Or, you can pass an array:

function theFuntion(myVa lues) {
....

var myData = ['anId','aCaller name','anAddres s','aPhone',
'aPhoneB','anEm ail','aSqft','t heSource',
'theAction','th eStatus'];
theFunction(myD ata);
...

You should also pass a reference to the form to make life
easier:

function theFuntion(theF orm, myValues) {
....

var myData = ['anId','aCaller name','anAddres s','aPhone',
'aPhoneB','anEm ail','aSqft','t heSource',
'theAction','th eStatus'];
theFunction(thi s.form, myData);
...

for(var i=0;i<myFields. length;i++) {
document.formna me.myFields[i].value=myValues[i];
}


Try this:

<script type="text/javascript">
function addValues(f, myValues){
var myFields = ['Id','callernam e','address','p hone',
'phoneb','email ','sqft','sourc e','action','st atus'];
for(var i=0, len=myFields.le ngth; i<len; i++) {
f.elements[myFields[i]].value = myValues[i];
}
}
</script>

<form action="">
<input name="Id" type="text" size="20">Id<br >
<input name="callernam e" type="text" size="20">calle rname<br>
<input name="address" type="text" size="20">addre ss<br>
<input name="phone" type="text" size="20">phone <br>
<input name="phoneb" type="text" size="20">phone b<br>
<input name="email" type="text" size="20">email <br>
<input name="sqft" type="text" size="20">sqft< br>
<input name="source" type="text" size="20">sourc e<br>
<input name="action" type="text" size="20">actio n<br>
<input name="status" type="text" size="20">statu s<br>
<input type="button" value="Add values" onclick="
var theValues = ['anId','aCaller name','anAddres s',
'aPhone','aPhon eB','anEmail',' aSqft',
'theSource','th eAction','theSt atus'];
addValues(this. form,theValues) ;
"><br>
<input type="reset">
</form>


--
Rob
Jul 23 '05 #2
Scott wrote:
I need to write a function that copies variables to fields. I've used an array and loop because it's neater than writing a similar sentence out 10 times.

var myString = new
String("id,call ername,address, phone,phoneb,em ail,sqft,source ,action,status" ); var myFields = myString.split( ',');

//myValues is an array of values passed to the function
var myValues =
[id,callername,a ddress,phone,ph oneb,email,sqft ,source,action, status];

for(var i=0;i<myFields. length;i++) {
document.formna me.myFields[i].value=myValues[i];
}

Firefox returns 'document.edit. myFields' has no properties. IE doesnt like it either. I'm sure this is a simple question but Google has turned up nothing :(


Just use a simple object---> { name: value , name: value....} to store
the data. It's an analogue of the form structure you're populating;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled </title>
<script type="text/javascript">

var elvals =
{
id: 'Mr. Id' ,
callername: 'Mrs. Caller' ,
address: '22 Belgravia' ,
phone: '444-657-6797' ,
phoneb: '555-769-0755' ,
email: 'b***@feh.net' ,
sqft: '246' ,
source: 'the Sun' ,
action: 'the Movement' ,
status: 'shmatus'
}

window.onload = function()
{
var el, f = document.forms. edit;
for (elname in elvals)
if (el = f.elements[elname])
el.value = elvals[elname];
}

</script>
</head>
<body>
<form name="edit">
<ul>
<li><input type="text" name="id" value="">__id</li>
<li><input type="text" name="callernam e" value="">__name </li>
<li><input type="text" name="address" value="">__addr ess</li>
<li><input type="text" name="phone" value="">__phon e</li>
<li><input type="text" name="phoneb" value="">__phon e 2</li>
<li><input type="text" name="email" value="">__emai l</li>
<li><input type="text" name="sqft" value="">__sq. ft.</li>
<li><input type="text" name="source" value="">__sour ce</li>
<li><input type="text" name="action" value="">__acti on</li>
<li><input type="text" name="status" value="">__stat us</li>
</ul>
</form>
</body>
</html>
I'm sure this is a simple question but Google has turned

up nothing :(

That's because it's in the FAQ...

http://jibbering.com/faq/faq_notes/square_brackets.html

Jul 23 '05 #3

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

Similar topics

4
1776
by: TempMan | last post by:
I want this text field to always display a number variable. The variable "num" is defined in the head, how can I get a text box to display this varibale?? <input name="Balance" type="text" value="num" size="8" maxlength="8"> Cheers
7
8695
by: Mike | last post by:
I've been trying for the past week to put a simple code together. I have done a LOT of searching, found scripts showing the functions I would like to use, however when I mix them it all goes wrong, somehow I always end up with error messages and functions not working right. Can someone please help me? I have a form, inside is 1 Text Field and 2 Password Fields. What I'm looking to do is: - Make sure password fields are equal - Set...
6
1619
by: Brian | last post by:
Help.. We cannot get this script to work..can someone take a look at it and make suggestions? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Convert Temperature</title>
7
2954
by: michael | last post by:
apologies in advance, as not only am i new to learning how to code javascript properly, i'm new to the groups posting thing... i am developing in firefox 1.0+, but will be working in an msie 6.0+ produciton environment ( not my choice, but when is it ever? ). the desired output is that when the end-user selects two radio buttons, one from each 'group', the form / page will open an alert window displaying the values of the radio buttons...
6
1863
by: J Mox | last post by:
I have a function that changes which radio button is selected. I need to pass the form name to the function but not the field name and am doing so like: changeRadio(this.form); The function: changeRadio(formname){ // I have skipped how the variable fieldname is created all that is important is that it needs to be
4
2684
by: Chris | last post by:
Hi, I am trying to create a popup calender so a user can click on a button on the main form, a calender will then popup, the user will select a date and then click ok on the popup. The date will then be passed back to the main form and populate a textbox. I tried two options. None worked. First. I used the popup calender form the time tracker starter kit. On my page I have an image buton. In the click event I have the foll code
5
2042
by: Chris | last post by:
Hi, I have been trying for days, yes days, to use the calender from the asp.net timetracker starter kit. I am now frustrated. I finally get the popup calender to popup but when I click ok to insert the date to the calling page I get the error "A runtime error has occured Line 14
4
2701
by: JV | last post by:
I thought I once saw somewhere that a global variable, "xyz" for example, could be declared and used so that instead of using "document.formname.elementname.value" one could use "xyz.elementname.value" or use "xyz" in place of "document.formname" anytime a reference to a form element was made. Is this actually true or not? I'm going crazy trying to recall something that might not even exist! Thanks.
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.