3

I had a content type=score This "score" content type had "field 1", "field 2" in integer. Can I customize the font color based on variables.

ex: when the users fill in <50 then show red color, When the users fill 50-70 then show orange color, When the users fill >70 then show green color.

Any contributed modules can do that function? Thanks

1 Answer 1

1

Yes, you can achieve this with jQuery.

In a custom module add the following code.

function custommodule_form_alter(&$form,$form_state,$form_id) {
  if ($form_id == "yourcontent type form") {
    $form['field_field1']['#attributes'] = array('id' => 'field1_id');
    drupal_add_js(drupal_get_path('module', 'custommodule') . '/jsfilename.js', ));    
  }
}

Put this in jsfilename.js.

(function($) {
  Drupal.behaviors.custommodule = {
    attach: function (context, settings) {
      // I suppose your field1 html elemet ID=field1_id
      $('#field1_id').change(function() {
        if($('#field1_id').val() < 50) {
          $(this).css("color","yellow"); // you can add class to it and in css file style it  ex:$(this).addClass('class1');
        }
        else if($('#field1_id').val() > 50 && $('#field1_id').val() < 70) {
          $(this).css("color","red");
          $(this).css("color","yellow"); // you can add class to it and in css file style it  ex:$(this).addClass('class1');
        }
        // …  
      });
    }
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.