Change color of field based on the value

I have a form where user selects values from a drop down in two fields. I added a new field to show the gap (field1 - field2). An ajax event (OnChange) on both fields updates the value of the gap when the filed is changed and all works fine.
How do I apply different colors to the field.
if gap == 1 then red
if gap == 2 then blue
if gap == 3 then green
I tried using sc_field_style({gap}, “#FF0000”, “30”, “#000000”, “”, “bold”);
but this is only valid for grid applications and I get Fatal error : Uncaught Error: Call to undefined function sc_field_style() in …

Any suggestion?

i think you have to use a single bracket

sc_field_style({titel_plus_sub}, ‘[kleur_categorie]’, ‘15px’, ‘#000000’, ‘Arial, sans-serif’, ‘bold’);

I am not sure where I have an extra bracket. Below is the actual code used in Ajax event On Change in the form, but I still get
Fatal error : Uncaught Error: Call to undefined function sc_field_style() in…

	if ({current_gap}== 3){
		sc_field_style({current_gap}, "#42c264", "30", "#000000", "Arial, sans-serif", "bold");
	}
	if ({current_gap}== 2){
		sc_field_style({current_gap}, "'#fdd481", "30", "#000000", "Arial, sans-serif", "bold");
	}
	if ({answers.priority}== 1){
		sc_field_style({current_gap}, "#fb9c77", "30", "#000000", "Arial, sans-serif", "bold");
	}
	if ({answers.priority}== 0){
		sc_field_style({current_gap}, "#FF0000", "30", "#000000", "Arial, sans-serif", "bold");
	}

you use “” and you should use ‘’

I have changed the double quote (") to single quote (’) and still the same
sc_field_style({current_gap}, ‘#FF0000’, ‘30’, ‘#000000’, ‘Arial, sans-serif’, ‘bold’);

The problem is that Scriptcase does not recognize the Function. I suspect it is a bug

Hello,
Sc_field_style just valid in grid, not form. see doc
https://www.scriptcase.net/docs/en_us/v9/manual/14-macros/02-macros/

Guy

HI, I suggest to use this function in your ajax event:

sc_ajax_javascript( ‘JavascriptMethodName’, array(“parameter”))

make a javascript function with two parameters, filedname and color, and via javascript change color of the field.

Hi Guy
I see the documentation shows grid only but when looking at the available macros in PHP methods for the form, these options are available. Yet the don’t work/ A bit confusing !!!

HI, I am not good at javascript at all. Is there a way that you possible could show the JavaScript code for me?

OK some internet search and self teaching I found the solution.
The trick was passing the parameter and then changing the color using the style attribute
document.F1.fieldname.style.fontWeight = ‘bold’;
document.F1.fieldname.style.color =‘yellow’;
document.F1.fieldname.style.backgroundColor =’#f4e80e’;

2 Likes

Nico, not very god at JS either. Is it possible to post a screen capture of the code, to help me see the exact statements and its placement?

HI @lewis200
Hope this helps


Nico, ty, I appreciate your effort. For some reason this is not working. DoI understand the field to be formatted is inserted in the array as both dimensions?

here is my code, but it doesnt change anything

THis is in js function called. setactive
document.F1.customerAccount.style.color = ‘blue’;
document.F1.customerAccount.style.backgroundColor = ‘yellow’;

This is onLoad
sc_ajax_javascript(‘setactive’, array({customerAccount}, {customerAccount}));

HI @lewis200

No not required at all. This is fault in my side and sc_ajax_javascript( ‘update_heat’, array({current_gap})); works just as good for me.
I assume that customerAccount is the field value in the form that you want to change.
I don’t see anything wrong with the code except for the quotes ’ which are not the same. is should be ‘blue’ and not ‘blue’
Send screen shot please
Also have a look at https://www.youtube.com/watch?v=QxLijWbZhF8&feature=youtu.be an excellent tutorial on using it.