Setting a field value in JS

Hello All,

I have created a string of value like the format below

=@~@=

eg : date=10/oct/2008@~@age=31

I am passing this string to the function below

function setFilterCriteria(fldValues)

{

   var splitVals = fldValues.split("@~@")

var newVal;

var fldName;

var fldVals;

var equalPos;





for (var i = 0;i < (splitVals.length-1);i++)

{

	newVal = splitVals[i].split("=")

	fldName = newVal[0]

	fldVals = newVal[1]

	document.all.newVal[0].value = newVal[1]

}

}

My requirement is to place the value in the corresponding field mentioned in the string.

In the above function i am splitting the values based on the @~@ first and using a for loop to split each value by = [ equal to ].

Now the newVal[0] will contain the field name and the newVal[1] will contain the field value. When i try to access and set the field value as document.all.newVal[0].value = newVal[1], i am getting error.

Can anyone guide me/ suggest me a work around to fix this problem.

Thanks in advance

Subject: Setting a field value in JS

Try

document.forms[0].elements[newVal[0]].value = newVal[1]

Subject: RE: Setting a field value in JS

Thanks for the quick response. I tried your work around. Even now its not working

Subject: RE: Setting a field value in JS

Are the fields editable? Otherwise you cannot set their value (at least, not with javascript)

Also, in the line

for (var i = 0;i < (splitVals.length-1);i++)

i < (splitVals.length-1) should be

i <= (splitVals.length-1)

or

i < splitVals.length

Even better is

var splitValsLength = splitVals.length

for (var i = 0;i < splitValsLength);i++)

If you use .length in the loop, its value will be recalculated every time you raise i

Subject: RE: Setting a field value in JS

Greetings,

You might need to send us the error because I got this code to work:

function setFilterCriteria(fldValues)

{

fldValues = “date=10/oct/2008@~@age=31”;

var splitVals = fldValues.split(“@~@”)

var newVal;

var fldName;

var fldVals;

var equalPos;

for (var i = 0;i < (splitVals.length);i++)

{

  newVal = splitVals[i].split("=");

  fldName = newVal[0];

  fldVals = newVal[1];

  document.all[newVal[0]].value = newVal[1];

}

}

Subject: RE: Setting a field value in JS

There’s never an adequate excuse for using document.all.

Subject: RE: Setting a field value in JS

I concur. I didn’t want to confuse the person though…

Subject: RE: Setting a field value in JS

Thanks for your suggestion rene and Irving.

I replaced my code

document.all.newVal[0].value = newVal[1];

as suggested by irving

(i.e) document.all[newVal[0]].value = newVal[1];

Now its working thanks a lot