concatenation

Questions related to customising nuBuilder Forte with Javascript or PHP.

concatenation

Postby JEDV » Wed Oct 24, 2018 11:15 pm

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?
JEDV
 
Posts: 9
Joined: Sun Oct 07, 2018 4:25 am

Re: concatenation

Postby kev1n » Wed Oct 24, 2018 11:27 pm

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.
kev1n
 
Posts: 203
Joined: Mon Oct 15, 2018 2:13 am

Re: concatenation

Postby JEDV » Thu Oct 25, 2018 6:48 am

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.
JEDV
 
Posts: 9
Joined: Sun Oct 07, 2018 4:25 am

Re: concatenation

Postby kev1n » Thu Oct 25, 2018 6:52 am

correction of the js code:
Code: Select all
field3.value = field1.value + " " + field2.value; var event = new Event('change'); field3.dispatchEvent(event);
kev1n
 
Posts: 203
Joined: Mon Oct 15, 2018 2:13 am

Re: concatenation

Postby admin » Thu Oct 25, 2018 11:21 am

.
admin
Site Admin
 
Posts: 3097
Joined: Mon Jun 15, 2009 9:53 am

Re: concatenation

Postby JEDV » Sun Feb 10, 2019 4:51 am

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);
JEDV
 
Posts: 9
Joined: Sun Oct 07, 2018 4:25 am


Return to Custom Code



cron