Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Color of a field based on value selection from dropdown

Questions related to using nuBuilder Forte.
sandeepgumudelli
Posts: 41
Joined: Thu Jan 25, 2018 3:51 pm

Color of a field based on value selection from dropdown

Unread post by sandeepgumudelli »

Hi,

Can this be possible to achive?

Background color of the field need to be changed after save based on the value selection from drop down.

Thank you very much for your help in advance.

Regards,
Sandeep
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Color of a field based on value selection from dropdown

Unread post by toms »

Hi,

Place this code to your form’s Javascript section (Options Menu -> Form Properties -> Custom Code -> JS)

Code: Select all

function colorSelect(f) {
  
	var val = $('#' + f).val();
	var bgcolor;
  
	switch(val) {
		case '0':
			bgcolor = 'green';
			break;
		case '1':
			bgcolor = 'red';
			break;
		default:
			bgcolor = 'white';
	}
  
	$('#' + f).css('background-color', bgcolor);
	$('#' + f + ' option').css('background-color', 'white');
	$('#' + f + ' option').css('color', 'black');
  
}

if (nuFormType() == 'edit' && nuCurrentProperties().record_id != '-1') {
   colorSelect('YOUR_FIELD');  // <--------------  Replace with your field ID
}
sandeepgumudelli
Posts: 41
Joined: Thu Jan 25, 2018 3:51 pm

Re: Color of a field based on value selection from dropdown

Unread post by sandeepgumudelli »

Thank you very much quick response tom. It is working perfectly with in the form. However, when i tried it in subform nothing happening. any suggestion please?

I tried keeping the code in main form adding subform_id.field_id in thelast line of code. but no luck
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Color of a field based on value selection from dropdown

Unread post by toms »

Is your subform type "Grid" or "Form" ?
subform_type.png
You do not have the required permissions to view the files attached to this post.
sandeepgumudelli
Posts: 41
Joined: Thu Jan 25, 2018 3:51 pm

Re: Color of a field based on value selection from dropdown

Unread post by sandeepgumudelli »

it's grid
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Color of a field based on value selection from dropdown

Unread post by toms »

Try something like this (untested):

Code: Select all

function colorSelect(f) {
  
   var val = $('#' + f).val();
   var bgcolor;
  
   switch(val) {
      case 'OK':
         bgcolor = 'green';
         break;
      case '1':
         bgcolor = 'red';
         break;
      default:
         bgcolor = 'white';
   }
  
   $('#' + f).css('background-color', bgcolor);
   $('#' + f + ' option').css('background-color', 'white');
   $('#' + f + ' option').css('color', 'black');
  
}

if (nuFormType() == 'edit' && nuCurrentProperties().record_id != '-1') {

 $('[id ^=sub_monthly_emp_status][id $=emp_status]').each(function () {
         colorSelect($(this).attr('id'));
     }) 
}	 

Last edited by Anonymous on Mon Aug 13, 2018 6:31 pm, edited 1 time in total.
sandeepgumudelli
Posts: 41
Joined: Thu Jan 25, 2018 3:51 pm

Re: Color of a field based on value selection from dropdown

Unread post by sandeepgumudelli »

just removed on closing } and tried with code by replacing sub_form_id and field_id but no luck.

Code: Select all

function colorSelect(selector) {

   var val = $(selector).val();
   var bgcolor;

   switch (val) {
      case 'ABC':
         bgcolor = 'green';
         break;
      case 'DEF':
         bgcolor = 'red';
         break;
      default:
         bgcolor = 'white';
   }

   $(selector).css('background-color', bgcolor);
   $(selector + ' option').css('background-color', 'white');
   $(selector + ' option').css('color', 'black');

}

if (nuFormType() == 'edit' && nuCurrentProperties().record_id != '-1') {

   $('[id ^=sub_monthly_emp_status][id $=emp_status]').each(function () {
         colorSelect($(this).attr('id'));
      }
  ) }
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Color of a field based on value selection from dropdown

Unread post by toms »

I corrected my post above
sandeepgumudelli
Posts: 41
Joined: Thu Jan 25, 2018 3:51 pm

Re: Color of a field based on value selection from dropdown

Unread post by sandeepgumudelli »

Thank you very much Tom. It is working perfect.. Once again thanks.
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Color of a field based on value selection from dropdown

Unread post by toms »

Nice!
Post Reply