Expand my Community achievements bar.

concatenating values from two fields into one field

Avatar

Level 2

Hi everyone,

I have two text fields at the top of my form for the user to type in their first and last names. I originally set the two fields to be global so that when I placed two identically named fields in the footer the values would automatically be repeated in that area across the document.  This has worked but I've since decided though that instead of having two separate read only fields in the footer, I'd like to run some javascript so that the combined first and last names will be entered into one field only.

I put the following code on the text field in the footer:

var first = student-info.firstname.rawvalue;

var last = student-info.lastname.rawvalue;

Concat(first, Space(1), last);

This hasn't work though.

My knowledge of javascript is quite limited so I wondered if someone could tell me where I've gone wrong in the code?

Appreciate any assistance.

11 Replies

Avatar

Former Community Member

The Concat function is a formcalc function not javascript.

To do this in javascript use this:

Fieldname where you want to put the result.rawValue = first + " " + last

Paul

Avatar

Level 2

Thanks for the reply,

I selected the "fullname" field in the footer and then in the scripts editor I chose "form:ready*" from the show drop down menu and then chose javascript for the language. The following is now the current code:

var first = student-info.firstname.rawValue;

var last = student-info.lastname.rawValue;

fullname.rawValue = first + " " + last;

When I test though, it still doesn't work. I'm wondering two things: first, is form:ready the right setting under "show" and should there be a more absolute path before the name of the text field "fullname"?

The "fullname" text field is on the master page, while the firstname and lastname fields are on page one so I'm not sure if the path is correct?

Can you comment further?

Avatar

Former Community Member

I assume that your script is on the form Ready of the fullname field. If so then you can replace the fullname.rawValue with this.rawValue. The "this" refers to the field that is executing the code. The event formReady fires when the form ios handed over to the user after initializing....so unless you are merging data into the form from the beginning or there are default vaues for these fields this code will not do much. If you expect to type in values into those fields and want them to appear in the Fullname field, move your code from the FormReady to the Calcualte event. The Calculate event will fires each time the first or last name field cahnges.

Paul

Avatar

Level 2

Ok I selected the fullname field and changed its event to calculate and the code is now as follows:

var first = WrapperSubform.studentinfo.firstname.rawValue;

var last = WrapperSubform.studentinfo.lastname.rawValue;

this.rawValue = first + " " + last;

All is working well even on the master page, except for one problem. When the form initially opens, this fullname field contains the words null null. Is there a way to eliminate this?

Avatar

Former Community Member

If the code is on the same field that you are populating then it shoudl work.

You can place the cursor in the script where you want the reference to occur and then hold down the ctrl hey and mouse to the field you want as you hover over fields the cursor will change to a V. When you get to the field you want click the mouse and the reference will be placed into the script editor where the cursor is located.

Paul

Avatar

Level 2

Sorry I ended up editing my last post but will explain here:

I found out that I needed to make the path more specific, eg.

var first = WrapperSubform.studentinfo.firstname.rawValue;

var last = WrapperSubform.studentinfo.lastname.rawValue;

So when I added the name of the parent subform, ie. WrapperSubform, it ended up working.

When the form initially opens though, this fullname field contains the words null null. Is there a way to eliminate this?

Avatar

Former Community Member

Yes ...you can test to see if those fields are a null and only execute the concatination if thet are not null .....something like this:

if (WrapperSubform.studentinfo.firstname.rawValue != null) || (WrapperSubform.studentinfo.lastname.rawValue != null){

     this.rawValue = WrapperSubform.studentinfo.firstname.rawValue + " " + WrapperSubform.studentinfo.lastname.rawValue

}

Note that I am not assigning the values of first and lastname to the variables that you have ...there is no need to do that.

Or you can simply give those two fields an initial value of a blank and then the concatination woudl be blank blank and woudl be invisible to th euser.

Paul

Avatar

Level 2

Thanks again - I tried the conditional code but this didn't seem to work.

I then tried your second suggestion by entering a space into the default field for the first and lastname fields. This achieved the effect but is that what you actually meant in order to set the initial value as blank, ie. does the space mean "blank"?

Avatar

Former Community Member

Yes ...a space in the field will eliminate the null...if you want to pursue the other method let me know. You can send the form to LiveCycle8@gmail.com and I will have a look at your code.

Paul

Avatar

Level 2

Thanks - I've emailed you the file. It would be good to get the conditional code working.