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?
Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
concatenation
-
- nuBuilder Team
- Posts: 4305
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 446 times
- Contact:
Re: concatenation
You need to trigger the change event.
With pure JavaScript:
Or event shorter with jQuery:
This will add the class nuEdited to the field3. Only fields that have this class will be saved to the DB.
With pure JavaScript:
Code: Select all
var event = new Event('change'); element.dispatchEvent(event); field3.value = field1.value + " " + field2.value;
Code: Select all
$('#field3').val($('#field1').val() + " " + $('#field2').val()).change();
-
- Posts: 9
- Joined: Sat Oct 06, 2018 8:55 pm
Re: concatenation
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.
-
- nuBuilder Team
- Posts: 4305
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 446 times
- Contact:
Re: concatenation
correction of the js code:
Code: Select all
field3.value = field1.value + " " + field2.value; var event = new Event('change'); field3.dispatchEvent(event);
-
- Posts: 9
- Joined: Sat Oct 06, 2018 8:55 pm
Re: concatenation
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);
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);