Concatenate Numbers like Text in Javascript

I have read and searched but am not able to get this to work for me. I read the following post by Andre and thought what I have would work, but it does not.

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/6ca412ae86b660a385256d83000463cb?OpenDocument&Highlight=0,javascript,string,number

Basically, I have three fields, all text, one for month, day, and year. I have a JS function what puts the fields together and then transfers them to a hidden text field. I am transering and then clearing out my input fields.

When I do:

cat = document.forms[0].RET1_A.value + “-” + document.forms[0].RET2_A.value + “-” + document.forms[0].RET3_A.value ;

alert('concat value: ’ + cat);

I get the value as expected 12-25-2006

But when I do:

eval(‘document.forms[0].TempReturnDate_A_’+what+‘.value=’ + cat);

I get -2019 (it does the math).

I tried addded ‘’ to be beginning of the string and I tried to change my seperator to a ~ or any alpha char and i get an error expected “;”.

I have tried to make all my vars strings but nothing. What can I do so that use “-” as a seprator?

Subject: Concatenate Numbers like Text in Javascript

One solution (of many) would be

var frm = document.forms[0];

var strDate = new Array(3);

strDate[0] = frm.RET1_A.value;

strDate[1] = frm.RET2_A.value;

strDate[2] = frm.RET3_A.value;

alert(‘concat value: ’ + strDate.join(’-');

Subject: RE: Concatenate Numbers like Text in Javascript

Thanks for the reply, but according to testing and Stan, it is still going to evaluate to do math. I posted my code in my reply to him explaining why I am using eval() and how.

Any other ideas would be grand.

Subject: Concatenate Numbers like Text in Javascript

It’s the eval() that’s killing you. Your ‘cat’ variable is a string that represents a mathematical expression, so it gets evaluated. Luckily, you don’t have to use eval(). (It’s actually rare that you are forced to rely on eval() for anything in JS; most of the examples you see on the web are actually examples of bad or lazy programming.)

In this case, you can use the “elements” collection of the form object to grab a field dynamically:

document.forms[0].elements[‘TempReturnDate_A_’ + what].value = cat;

Subject: RE: Concatenate Numbers like Text in Javascript

Acutally I am using eval() becuase I have 1 set of input fields and 12 value holder fields. I am using the eval() function and passing it the place holder number.

Here is my function:

function transfer(what)

{

cat = document.forms[0].RET1_A.value + “-” + document.forms[0].RET2_A.value + “-” + document.forms[0].RET3_A.value ;

eval(‘document.forms[0].FName_A_’+what+‘.value=document.forms[0].FName_A.value’);

eval(‘document.forms[0].MIntial_A_’+what+‘.value=document.forms[0].MIntial_A.value’);

eval(‘document.forms[0].LName_A_’+what+‘.value=document.forms[0].LName_A.value’);

eval(‘document.forms[0].Email_A_’+what+‘.value=document.forms[0].Email_A.value’);

eval(‘document.forms[0].EmailC_A_’+what+‘.value=document.forms[0].EmailC_A.value’);

eval(‘document.forms[0].DOB1_A_’+what+‘.value=document.forms[0].DOB1_A.value’);

eval(‘document.forms[0].DOB2_A_’+what+‘.value=document.forms[0].DOB2_A.value’);

eval(‘document.forms[0].DOB3_A_’+what+‘.value=document.forms[0].DOB3_A.value’);

eval(‘document.forms[0].Relationship_A_’+what+‘.value=document.forms[0].Relationship_A.value’);

eval(‘document.forms[0].TempAddress_A_’+what+‘.value=document.forms[0].TempAddress_A.value’);

eval(‘document.forms[0].TempFName_A_’+what+‘.value=document.forms[0].TempFName_A.value’);

eval(‘document.forms[0].TempMIntial_A_’+what+‘.value=document.forms[0].TempMIntial_A.value’);

eval(‘document.forms[0].TempLName_A_’+what+‘.value=document.forms[0].TempLName_A.value’);

eval(‘document.forms[0].TempAddress1_A_’+what+‘.value=document.forms[0].TempAddress1_A.value’);

eval(‘document.forms[0].TempAddress2_A_’+what+‘.value=document.forms[0].TempAddress2_A.value’);

eval(‘document.forms[0].TempCity_A_’+what+‘.value=document.forms[0].TempCity_A.value’);

eval(‘document.forms[0].TempState_A_’+what+‘.value=document.forms[0].TempState_A.value’);

eval(‘document.forms[0].TempZip_A_’+what+‘.value=document.forms[0].TempZip_A.value’);

eval(‘document.forms[0].TempZipFour_A_’+what+‘.value=document.forms[0].TempZipFour_A.value’);

eval(‘document.forms[0].TempPhone1_A_’+what+‘.value=document.forms[0].TempPhone1_A.value’);

eval(‘document.forms[0].TempPhone2_A_’+what+‘.value=document.forms[0].TempPhone2_A.value’);

eval(‘document.forms[0].TempPhone3_A_’+what+‘.value=document.forms[0].TempPhone3_A.value’);

eval(‘document.forms[0].TempReturnDate_A_’+what+‘.value=’ + cat);

eval(‘document.forms[0].TempCorrespondence_A_’+what+‘.value=document.forms[0].TempCorrespondence_A.value’);

I then want to take the three text input date fields and put them together into one text place holder date field. I would like to use the “-” as separate becuase later I may have to recall the field values and would like to use the split() function to parse them back using the “-” rather then worrying about place holders, etc.

Is there a way around the eval() function reading my value as a math bur rather as text?

Subject: RE: Concatenate Numbers like Text in Javascript

But you don’t have to. Here’s the same function without eval():

function transfer(what) {

var cat = cat = document.forms[0].RET1_A.value + “-” + document.forms[0].RET2_A.value + “-” + document.forms[0].RET3_A.value ;

var f = document.forms[0];

f.elements[‘FName_A_’+what].value = f.FName_A.value;

f.elements[‘MIntial_A_’+what].value=f.MIntial_A.value;

f.elements[‘LName_A_’+what].value=f.LName_A.value;

f.elements[‘Email_A_’+what].value=f.Email_A.value;

f.elements[‘EmailC_A_’+what].value=f.EmailC_A.value;

f.elements[‘DOB1_A_’+what].value=f.DOB1_A.value;

f.elements[‘DOB2_A_’+what].value=f.DOB2_A.value;

f.elements[‘DOB3_A_’+what].value=f.DOB3_A.value;

f.elements[‘Relationship_A_’+what].value=f.Relationship_A.value;

f.elements[‘TempAddress_A_’+what].value=f.TempAddress_A.value;

f.elements[‘TempFName_A_’+what].value=f.TempFName_A.value;

f.elements[‘TempMIntial_A_’+what].value=f.TempMIntial_A.value;

f.elements[‘TempLName_A_’+what].value=f.TempLName_A.value;

f.elements[‘TempAddress1_A_’+what].value=f.TempAddress1_A.value;

f.elements[‘TempAddress2_A_’+what].value=f.TempAddress2_A.value;

f.elements[‘TempCity_A_’+what].value=f.TempCity_A.value;

f.elements[‘TempState_A_’+what].value=f.TempState_A.value;

f.elements[‘TempZip_A_’+what].value=f.TempZip_A.value;

f.elements[‘TempZipFour_A_’+what].value=f.TempZipFour_A.value;

f.elements[‘TempPhone1_A_’+what].value=f.TempPhone1_A.value;

f.elements[‘TempPhone2_A_’+what].value=f.TempPhone2_A.value;

f.elements[‘TempPhone3_A_’+what].value=f.TempPhone3_A.value;

f.elements[‘TempReturnDate_A_’+what].value= cat;

f.elements[‘TempCorrespondence_A_’+what].value=f.TempCorrespondence_A.value;

}

Subject: RE: Concatenate Numbers like Text in Javascript

Made the changes as you suggested this morning and it worked as planned.

Thank you again, Stan. I really appreciate the help that you have been giving me.