Computer >> Computer tutorials >  >> Programming >> Javascript

How to set the font family for text with JavaScript?


To set the font family, use the fontFamily property in JavaScript.

Example

You can try to run the following code to set the font family for text with JavaScript −

<!DOCTYPE html>
<html>
   <body>
      <h1>Heading 1</h1>
      <p id="myID">
         This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
         This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
      </p>
      <button type="button" onclick="display()">Set Font Family</button>
      <script>
         function display() {
            document.getElementById("myID").style.fontFamily = "verdana,sans-serif";
         }
      </script>
   </body>
</html>