Is there a way to change a font color in Lightning input rich text field when maximum character limit is exceeded in LWC JS?

4.5k views Asked by At

I'm trying to get the character count and on that basis want to change the color of font.

I have tried with queryselector() but it is not working:

handleChange(event) {
         if( this.charCount > 100){        
              let textcolor = this.template.queryselector("lightning-input-rich-text");
                 textcolor.getformat({color:"red"});
              }
        }
        

I have also tried with:

        handleChange(event) {
         if( this.charCount > 100){  
               textcolor = this.template.queryselector('[data-id="summary"]');
           if(textcolor) 
                    this.template.querySelector('[data- 
 id="summarytextcolor"]').className='summarytext';
                 
        }
        }

Here is my CSS:

.summarytext {color: #ff0000;}

And here is my lightning code, where I have taken a rich text field where on change I need to capture the number of character count:

<lightning-input-rich-text
         data-id="summary"
        label={label.Summary}
        formats={richTextformats}
        label-visible
         valid={richTextValidity}
         message-when-bad-input={richTextErrorMessage}
        class="slds-form-element slds-form-element_stacked summarytext"
        onchange={handleChange}
        value={richTextValue}
        style=" font-weight: normal;">
    </lightning-input-rich-text>

Here is the error I'm getting:

[NoErrorObjectAvailable] Script error.    a()@https://static.lightning.force.com/cs10/auraFW/javascript/7p9HLMpgnV2GO9MqZhXGUw/aura_prod.js:948:169 {anonymous}()@https://static.lightning.force.com/cs10/auraFW/javascript/7p9HLMpgnV2GO9MqZhXGUw/aura_prod.js:948:362 bt.dispatchEvent()@https://static.lightning.force.com/cs10/auraFW/javascript/7p9HLMpgnV2GO9MqZhXGUw/aura_prod.js:12:12146 bt.dispatchChangeEvent()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/interop/inputRichText.js:2:28144 eval()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/interop/inputRichText.js:2:20578 e.l.emit()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/lightning/quillLib.js:2:142032 e.value()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/lightning/quillLib.js:2:33139

What's the problem, and how can I fix it?

1

There are 1 answers

0
Tom Reinman On

You should be able to set it using vanilla javascript.

handleChange(event) {
        this.loading = true;
        if(this.charCount > 100){        
             var warn = richTextValue.fontcolor("Red");
             this.richTextValue = warn;
        }
        this.loading = false;
}

I added a variable 'loading'; just make sure the input rich text is wrapped in a <template if:false={loading}> so the color of the font gets re-rendered.