We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Get the font color of selected range from the RichTextEditor’s content.

Hi,

I wondered how can i get the font color of selected range from the RichTextEditor’s content.

I tried to use the getRange() method, but it failed.

Could you help me?


8 Replies

VJ Vinitha Jeyakumar Syncfusion Team February 9, 2023 07:42 AM UTC

Hi ,


We have prepared a sample as per your requirement to get the font color of the selected text in the RichTextEditor.

Code snippet:
document.getElementById('dialogBtn').onclick = (): void => {
  var sel = window.getSelection();
  var range = sel.getRangeAt(0);
  var pointedTag = range.startContainer.parentNode;
  console.log(pointedTag.style.color);
};



Regards,
Vinitha




?? ??? February 9, 2023 08:48 AM UTC

Hi Vinitha,

Thank you for your answer.


I checked the answer well, but there is a problem that it doesn't always give the right value.

If change the color partially, i cannot receive the color value from pointedTag.

because the structure of the HTML element changes.

This is also true when applying other properties such as bold and italic.

Is there any other way?


Regards,

Jane



?? ??? February 9, 2023 09:39 AM UTC

And add,



As above,

Other toolbar items, such as BOLD and ITALIC, are being marked in the toolbar for the selected range.

But the font color is not.

(and I'm using inline mode in my Vue app.)


Is there a possibility that this will be modified?

or Can I know how to get the color value exactly like such as BOLD and ITALIC ?


Regards,

Jane



VJ Vinitha Jeyakumar Syncfusion Team February 10, 2023 01:07 PM UTC

Hi,


Query 1. "If change the color partially, i cannot receive the color value from pointedTag. because the structure of the HTML element changes. This is also true when applying other properties such as bold and italic.?"

We have modified the solution by applying some conditions as per your use case scenario. please check the sample below, similarly you can change the conditions as per your requirement.


Query 2. "Other toolbar items, such as BOLD and ITALIC, are being marked in the toolbar for the selected range. But the font color is not. Is there a possibility that this will be modified? or Can I know how to get the color value exactly like such as BOLD and ITALIC ?"

We want to let you know that the Font color toolbar item only shows the applied color at the bottom of the icon, and it won't get highlights like the bold and italic. This is the intended behavior of the Rich Text Editor control.

Regards,
Vinitha


?? ??? February 14, 2023 01:48 AM UTC

Hi Vinitha,

Thank you for your answer.


I tried to use the solution, but it has some problems...

So I want to know if there is any possibility to added a method in Syncfusion RichTextEditor that returns the font color of the selected range.

(such as getSelectedHtml, getSelection ...)


Regards,

Jane




VJ Vinitha Jeyakumar Syncfusion Team February 14, 2023 02:00 PM UTC

Hi Jane,

By default, we didn't provide any methods to get the font color of the selected text content in RichTextEditor. Can you please share us with the details of the problems you have faced at your end to use the suggested solution in our last update?

Regards,
Vinitha




?? ??? February 15, 2023 04:29 AM UTC

Hi  Vinitha,

Thank you for your answer.


If the selected range used more than one color, I want to know that.

so I checked when the first and last colors in selected range were different

using 'startContainer' and 'endContainer' in getRange Method.

by comparing those two colors.



But the problem is when the color of the text in the middle area of the selected range.

such as



Is there any solution?


Regards,

Jane



VJ Vinitha Jeyakumar Syncfusion Team February 15, 2023 12:02 PM UTC

Hi Jane,

Query "If the selected range used more than one color, I want to know that"

Your requirement to get the color of the selected multiple text colors can be achieved by using the below suggested solution using getSelectedHtml method,

Code snippet:
document.getElementById('dialogBtn').onclick = (): void => {
  var divElem = document.createElement('div');
  divElem.innerHTML = document
    .getElementById('defaultRTE')
    .ej2_instances[0].getSelectedHtml();
  for (var i = 0i < divElem.childNodes.lengthi++) {
    if (divElem.childNodes[i].tagName == 'SPAN') {
      console.log(divElem.childNodes[i].style.color);
    }
  }
};



Regards,
Vinitha

Loader.
Live Chat Icon For mobile
Up arrow icon