I have a form that does a lookup to retrieve many fields. I would like to change the color of any field that had been changed from the original lookup so those fields would stand out to a reviewer. Is there a way to do that with JavaScript? The fields that can change would include: Address, PhoneNumber, CellNumber..., stuff like that.
Question
Question
Answer
You should be able to do something like this, but you would need to do a small amount of code for each field (in particular, you probably need at least one boolean state values for each field). I just built a small test form and got it to work using this code:
$(document).ready(function(){ var manuallyFocused = false; $('#q2 input').on('focus',function(){ manuallyFocused=true;}); $('#q2 input').on('blur',function(){ manuallyFocused=false;}); $('#q2 input').on('change',function(){ if (manuallyFocused) { $('#q2 input').css('background','#ffff99'); } else { $('#q2 input').css('background','none'); } }); });
This works by intercepting 3 events: focus, blur, and change on the target field (q2 input in my case). If the field is modified by the lookup rule, a change event is triggered, but there is no corresponding focus or blur events on the field. If you manually change it, you first have to click on it which gives it focus. This sets the boolean which lets the change function know that it was manually changed.
Scott - When I use this code, it changes the color on look-up. Any ideas how this can be modified to work if changes have been made after the look-up?
To make this more concrete, let's assume that you have one field, say ID Number, that is the basis for the lookup. This value populates a second field, say Employee Name, after the lookup runs.
There are 2 issues that may prevent it from working:
1) If you tab or click off of the trigger field (ID Number) and move focus into the target field (Employee Name), then this won't work as expected. There are a few ways you could fix this. Put something else in between the lookup trigger and the target fields so that when you hit tab it doesn't interfere with the code. Otherwise, you'd need some fancy tricks to try to determine when the lookup code has completed running, and to not respond to any focus/change events until after the lookup is completely finished. Could be done, but it's not easy because the lookup doesn't provide any indication that it finishes. It just triggers changes on every field it touches.
2) If you have multiple fields using this code, but you don't change the variable name for manuallyFocused (you would want a unique name for it for each variable you are checking for changes). This could cause a problem where it flags a field as changed unintentionally.