help with document.formname.variable.value

S

Scott

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,callername,address,phone,phoneb,email,sqft,source,action,status");
var myFields = myString.split(',');

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

for(var i=0;i<myFields.length;i++) {
document.formname.myFields.value=myValues;
}

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 :(
 
R

RobG

Scott said:
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,callername,address,phone,phoneb,email,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','callername','address','phone',
'phoneb','email','sqft','source',
'action','status'
];

//myValues is an array of values passed to the function
var myValues =
[id,callername,address,phone,phoneb,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('anId','aCallername','anAddress','aPhone',
'aPhoneB','anEmail','aSqft','theSource',
'theAction','theStatus');
...

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

function theFuntion(myValues) {
....

var myData = ['anId','aCallername','anAddress','aPhone',
'aPhoneB','anEmail','aSqft','theSource',
'theAction','theStatus'];
theFunction(myData);
...

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

function theFuntion(theForm, myValues) {
....

var myData = ['anId','aCallername','anAddress','aPhone',
'aPhoneB','anEmail','aSqft','theSource',
'theAction','theStatus'];
theFunction(this.form, myData);
...
for(var i=0;i<myFields.length;i++) {
document.formname.myFields.value=myValues;
}


Try this:

<script type="text/javascript">
function addValues(f, myValues){
var myFields = ['Id','callername','address','phone',
'phoneb','email','sqft','source','action','status'];
for(var i=0, len=myFields.length; i<len; i++) {
f.elements[myFields].value = myValues;
}
}
</script>

<form action="">
<input name="Id" type="text" size="20">Id<br>
<input name="callername" type="text" size="20">callername<br>
<input name="address" type="text" size="20">address<br>
<input name="phone" type="text" size="20">phone<br>
<input name="phoneb" type="text" size="20">phoneb<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">source<br>
<input name="action" type="text" size="20">action<br>
<input name="status" type="text" size="20">status<br>
<input type="button" value="Add values" onclick="
var theValues = ['anId','aCallername','anAddress',
'aPhone','aPhoneB','anEmail','aSqft',
'theSource','theAction','theStatus'];
addValues(this.form,theValues);
"><br>
<input type="reset">
</form>
 
R

RobB

Scott said:
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,callername,address,phone,phoneb,email,sqft,source,action,status");
var myFields = myString.split(',');

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

for(var i=0;i<myFields.length;i++) {
document.formname.myFields.value=myValues;
}

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: '(e-mail address removed)' ,
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="callername" value="">__name</li>
<li><input type="text" name="address" value="">__address</li>
<li><input type="text" name="phone" value="">__phone</li>
<li><input type="text" name="phoneb" value="">__phone 2</li>
<li><input type="text" name="email" value="">__email</li>
<li><input type="text" name="sqft" value="">__sq. ft.</li>
<li><input type="text" name="source" value="">__source</li>
<li><input type="text" name="action" value="">__action</li>
<li><input type="text" name="status" value="">__status</li>
</ul>
</form>
</body>
up nothing :(

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top