- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2019 07:35 AM
Hi
I've written a short client script to change the colour of a selection value form field when the value changes. It works well, but I want to use the same code across quite a number of form fields. How can I put this code into a client side function that I can call for each of my form fields?
I looked at UI scripts, but my head exploded with the complexity of implementing these. Isn't there a simpler way to call a custom client side function?
Here's the onChange function I'd like to call for a variety of form fields:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var elementID = g_form.getElement(control.name);
switch(newValue) {
case "3": elementID.style.backgroundColor = "red"; break;
case "2": elementID.style.backgroundColor = "orange"; break;
case "1": elementID.style.backgroundColor = "yellow"; break;
default: elementID.style.backgroundColor = "white"; break; }
}
Thanks in advance.
David
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2019 02:13 PM
Hi
I'm delighted to report that I have, at last, resolved this issue. After weeks of trying different methods to load the functions I wanted to share between fields on a form (so I could call the same functions onChange on several form fields) I could get it to work by including my shared functions within <script></script> tags inside a UI Macro:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<script>
function calcgxpassessment() {
// My code here
}
</script>
</j:jelly>
Within my code, I had to rework things like && and change double quotes because these are XML entities. I haven't tried replacing these with the correct XML entities (E.g replacing && with &&)
I then included the UI Macro in a UI Formatter
and then dropped the UI formatter into the bottom of the form.
Finally, I was able to call the function calcgxpassessment() onChange from any choice field that needs to recalculate the risk. I also had to remember to deselect "Isolate script" from these client scripts. Otherwise, they couldn't see the shared function.
My project is back on the rails 🙂
Regards
David

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2019 07:50 AM
You could write a script include and call it with a Glide Ajax call. You could also write a function in an onLoad script but outside of the actual onLoad function like:
function onLoad() {
//Type appropriate comment here, and begin script below
}
function yourFunctionHere() {
//Do stuff
}
Then call the function within your onChange script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2019 08:12 AM
Hi Elijah
Thanks for your reply. I tried the onLoad suggestion as it seems the simpler of the two, but unfortunately this doesn't work. When the form is loaded, and the onChange fires for the form fields, I get this error:
onChange script error: ReferenceError: setColour is not defined function() { [native code] }
It appears that the onLoad hasn't loaded my function at the time the onChange is called?
This is my onLoad script:
function onLoad() {
// This function does nothing, but the code below gets loaded.
}
//
// Set the colour of a form field according to the value selected.
//
function setColour(control, newValue) {
var elementName = control.name;
var elementID = g_form.getElement(elementName);
switch(newValue) {
case "3": elementID.style.backgroundColor = "red"; break;
case "2": elementID.style.backgroundColor = "orange"; break;
case "1": elementID.style.backgroundColor = "yellow"; break;
default: elementID.style.backgroundColor = "white"; break;
}
}
And this is now my onChange script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
setColour(control, newValue);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2019 08:29 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2019 08:33 AM