Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating colorful text in JavaScript

I am trying to write a function that prints a certain text into a <div id="1"> tag.

The string should mark certain index values in different color.

What I have written now is to go to all the index values I have and add a <font color="color"> tag, and then I add it using div1.innerHTML = result;

Its a lot of work, and its very complicated. Is there another way that I can create a string object like I've described without these HTML tags?

If I can do that then I would just use div1.appendChild(String);

like image 950
yotamoo Avatar asked May 31 '11 15:05

yotamoo


People also ask

How do you make your text colorful?

You can change the color of text in your Word document. Select the text that you want to change. On the Home tab, in the Font group, choose the arrow next to Font Color, and then select a color. You can also use the formatting options on the Mini toolbar to quickly format text.

How do I change the color of something in JavaScript?

To change color in JavaScript, do this: element. style. color = 'red' .

How do you change the text color of an element?

To change some of the text in the HTML document to another color use the FONT COLOR Tag. To change the color of the font to red add the following attribute to the code to the <FONT COLOR=" "> tag. #ff0000 is the color code for red.

How do I change the font color in HTML script?

To set the font color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property color. HTML5 do not support the <font> tag, so the CSS style is used to add font color.


1 Answers

I generally am loathe to recommend that anybody use a library that they don't already claim to use, but this is one of those times where the question almost directly asks for a library as an answer :-)

Check out Lettering.JS. It was designed to do exactly what you describe. It wraps your text content by letter or by word or by line (I think) in <span> tags, under your control. You then use CSS to style elements, or some more JavaScript to manipulate and style the elements it creates for you.

like image 152
Pointy Avatar answered Oct 07 '22 07:10

Pointy