kendo-uikendonumerictextbox

Dyanamically change the font color of Kendo Numeric textbox


I am using Kendo Numeric text box to display decimal values.I want to change the font color based on the user input.How to achieve this?


Solution

  • You Can do this in JQuery easily by changing the style on the change and spin event of the input:

    On your numeric text box add the events:

     $("#numerictextbox").kendoNumericTextBox(
        {
            change: onChangeOrSpin,
            spin: onChangeOrSpin
       });
    

    Then in the event handler call:

      function onChangeOrSpin() 
      {
           var val = this.value();
            changeFontColour(val);
      } 
    
      function changeFontColour(val)
        {
            if(val < 5)
            {
                $("#numerictextbox, .k-input").css('font-family','Arial');
                $("#numerictextbox, .k-input").css('font-style','italic');
                $("#numerictextbox, .k-input").css('font-weight','bold');
                $("#numerictextbox, .k-input").css('color','blue');
            }
            else
            {
                  $("#numerictextbox, .k-input").css('font-family','Times New Roman');
                $("#numerictextbox, .k-input").css('font-style','normal');
               $("#numerictextbox, .k-input").css('font-weight','normal');
                $("#numerictextbox, .k-input").css('color','black');
    
            }
        }
    

    Here is a working fiddle: http://jsfiddle.net/loanburger/nwsw4yeq/

    Update:

    For multiple numeric inputs on the same page, use classes and add a different class to each input so you can distinguish between then when you need to change the colour. Then use a data attribute to find the correct one to change.

    You cannot use the id because there are two parts to how kendo creates the widget:

    1. The first part is the input for editing the value. This is the one the user interacts with. This input will be given the class: numerictextbox numericOne k-input
    2. There is a second input of interest which is the one used to display the value after the widget looses focus this one gets the classes: k-formatted-value numerictextbox numericOne k-input

    Neither of these inputs get an id and you don't want to use the native Kendo classes either so using a distinct class on each of your numeric widget controls, you can identify which colours to change since that class will get added to both inputs in the widget.

    See here: https://jsfiddle.net/loanburger/4evy3tcg/