Page 1 of 1

concatenation

PostPosted: Wed Oct 24, 2018 11:15 pm
by JEDV
I am trying to concatenate two string fields into a third string field on a add form using custom code in the object of a form.
object field2
event onblur [of field2]
javascript field3.value = field1.value + " " + field2.value;

This works beautifully and field3 displays the concatenated value on the form. However, on saving the record, the field3 value is not saved. Could someone point me to a source that would explain how to do this correctly?

Re: concatenation

PostPosted: Wed Oct 24, 2018 11:27 pm
by kev1n
You need to trigger the change event.

With pure JavaScript:

Code: Select all
var event = new Event('change'); element.dispatchEvent(event); field3.value = field1.value + " " + field2.value;


Or event shorter with jQuery:

Code: Select all
$('#field3').val($('#field1').val() + " " + $('#field2').val()).change();


This will add the class nuEdited to the field3. Only fields that have this class will be saved to the DB.

Re: concatenation

PostPosted: Thu Oct 25, 2018 6:48 am
by JEDV
Thanks Kevin. The Jquery worked great. The pure javascript did not, but I likely did not understand it. Time to work thru a javascript and jquery tutorial.

Re: concatenation

PostPosted: Thu Oct 25, 2018 6:52 am
by kev1n
correction of the js code:
Code: Select all
field3.value = field1.value + " " + field2.value; var event = new Event('change'); field3.dispatchEvent(event);

Re: concatenation

PostPosted: Thu Oct 25, 2018 11:21 am
by admin
.

Re: concatenation

PostPosted: Sun Feb 10, 2019 4:51 am
by JEDV
Kevin,
In order to get your javascript above to work I had to change the variable name "event" to event1. I suspect that "event" is a reserved word.

field3.value = field1.value + " " + field2.value; var event1 = new Event('change'); field3.dispatchEvent(event1);