• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Actual Wizard

Actual Wizard

Great Content From Actual Wizards

  • Blogging
  • Programming
  • Social Media
  • Marketing
You are here: Home / Programming / JavaScript

JavaScript String fontcolor Method Explained

August 18, 2020 by Kevin Marszalek

The HTML code to wrap text in a font tag can be generated using JavaScript.

MyHTML = MyText.fontcolor(color);

Important Note

The fontcolor() method is not standardized and may not behave consistently in all browsers.

The method produces HTML 4 code that utilizes a font tag that does not exist in HTML 5.

It should be avoided if possible, and CSS should be used instead. An example using CSS is provided here.

HTML 4 Font Attribute Value Types:

The old font attribute will accept three types of values:

  • The names of simple colors, such as red, blue, or green.
<font color="red">
  • A hexadecimal color code, proceeded by a pound sign.
<font color="#0000ff">
  • An RGB value, example: rgb(0,255,0)
<font color="rgb(0,255,0)">

JavaScript String fontcolor() Method Example

<!DOCTYPE html>
<html lang="en">
<head>
	<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
	<meta content="utf-8" http-equiv="encoding">
	<title>JavaScript String fontcolor() Method Example</title>
</head>
<body>
<script>
	var MyText = "Sample Text With the fontcolor HTML Tag";
	var MyHTML = MyText.fontcolor("red");
	document.write(MyHTML);
</script>
</body>
</html>

The code above produces a blank HTML document with the following HTML:

<font color="red">Sample Text With the fontcolor HTML Tag</font>

Alternative to Using the fontcolor() Method using CSS

Even though the most commonly used browsers treat the fontcolor method consistently, it is still better to avoid potential issues by creating the needed HTML code inside a string and then inserting that HTML into the DOM.

Example JavaScript fontcolor() Method Alternative:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
	<meta content="utf-8" http-equiv="encoding">
	<title>JavaScript String fontcolor() Method Alternative</title>
</head>
<body>
	<h1>An Example of How To Generate Colored Text with JavaScript</h1>
<script>
	var MyText = "Example of an alternative way to generate colored text with HTML in JavaScript.";
	var MyStyle = "<style>.blue-text{color: #0000ff;}</style>";
	var MyHTML = "<span class=\"blue-text\">" + MyText + "</span>";
	document.head.innerHTML += MyStyle;
	document.body.innerHTML += MyHTML;
</script>
</body>
</html>

Which will create the following HTML:

<span class="blue-text">Example of an alternative way to generate colored text with HTML in JavaScript.</span>

The example will also insert the following CSS style into the document’s header:

<style>.blue-text{color: #0000ff;}</style>

The Pure HTML & CSS Alternative

In the case that JavaScript can be completely avoided, an inline style can be used instead.

<span style="color: rgb(0,255,0)">Example of Green Colored Text with HTML and CSS.</span>

Using the fontcolor() Method With An Invalid Argument

Passing an invalid argument to this method does not generate an error message and will usually result in text that is colored green in the browser.

MyText.fontcolor("The Text Will Be Green");
//Invalid Usage of the Font Attribute

"This Text Will Also Be Green".fontcolor();
//This will set the Attribute value to "undefined"

"This Text Will be Black".fontcolor(123456789);
//123456789 isn't a valid color code.

Using the fontcolor() Method Without a Variable

It is worth mentioning that the fontcolor method can be called on a string that is not stored in a variable.

The following code will work but is discouraged because it can be confusing to read.

document.write('Red Text with the fontcolor() Method'.fontcolor("red"));

JavaScript will determine that the text within the quotes is a string and apply the fontcolor() method to it.

Filed Under: JavaScript

Read More From Actual Wizard

  • Understanding the Different Parts of an Email Address
    An email address has four parts; the recipient name, the @ symbol, the domain name, and the top-level domain. …
  • How to Get the Last Element of an Array in JavaScript
    There are a number of different ways to get the last element of an array in JavaScript. Below we will explore …
  • How to use document.write() in JavaScript
    How to use document.write() in JavaScript The document.write() function is commonly used when testing simple …
  • How to Open a New Tab with JavaScript
    Opening a new browser window in a new tab can be done easily in JavaScript. Modern web browsers provide a …

Primary Sidebar

More posts from this section

  • How to Get the Last Element of an Array in JavaScript
  • How to use document.write() in JavaScript
  • How to Open a New Tab with JavaScript
  • How to Convert an HTML Node to a String in JavaScript
  • JavaScript String Anchor Method Explained
  • JavaScript String Big Method Explained
  • JavaScript String Blink Method Explained
  • JavaScript String Bold Method Explained
  • JavaScript String charAt Method Explained
  • JavaScript String charCodeAt Method Explained

Copyright © 2025 ActualWizard.com

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy