Javascript Reference - JavaScript String fontcolor() Method








The fontcolor() method is used to display a string in a specified color by using the <font> tag.

Browser Support

fontcolor() Yes Yes Yes Yes Yes

Syntax

stringObject.fontcolor('color');

Parameter Values

Parameter Description
color Required. A color value. It could be color name(red), or an RGB value (rgb(255,0,0)), or a hex number (#FF0000).




Return Value

A string embedded in the <font> tag.

String with HTML tags

MethodOutput
anchor(name)<a name="name">string</a>
big()<big>string</big>
bold()<b>string</b
fixed()<tt>string</tt>
fontcolor(color)<font color="color">string</font>
fontsize(size)<font size="size">string
italics()<i>string</i>
link(url)<a href="url">string</a>
small()<small>string</small>
strike()<strike>string</strike>
sub()<sub>string</sub>
sup()<sup>string</sup>

The following code shows how to use the methods above.

<html>
<body>
    <script type="text/javascript">
      var myString = new String();
      myString = "java2s.com";
      document.writeln(myString.anchor("java2s.com"));
      document.writeln(myString.big());
      document.writeln(myString.bold());
      document.writeln(myString.fixed());
      document.writeln(myString.fontcolor(color));
      document.writeln(myString.fontsize(size));
      document.writeln(myString.italics());
      document.writeln(myString.link(url));
      document.writeln(myString.small());
      document.writeln(myString.strike());
      document.writeln(myString.sub());
      document.writeln(myString.sup());
<!--  w w w . j a  v a2 s .  co m-->
    </script>
</body>
</html>

Click to view the demo





Example

Display the text in green color:


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!-- w ww  .  j  a v  a 2  s  . c  o  m-->
<script>
function myFunction() {
    var str = "Hello World!";
    var result = str.fontcolor("green");
    document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

The code above is rendered as follows: