JavaScript - Simple Dynamic Change Font

In this tutorial we will create a Simple Dynamic Change Font using JavaScript. This code will dynamically change the website font when the user click the button. The code use onclick() function to initiate a certain method that can modify a target element font properties without configuring the css. This is a free program, you can modify it and use it as your own. We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple Dynamic Change Font</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-2"></div>
  17.                 <div class="col-md-8">
  18.                         <div class="form-inline">
  19.                                 <label>Select font: </label>
  20.                                 <select class="form-control" id="font">
  21.                                         <option value="Impact">Impact</option>
  22.                                         <option value="Charcoal">Charcoal</option>
  23.                                         <option value="Sans-Serif">Sans-Serif</option>
  24.                                         <option value="Verdana">Verdana</option>
  25.                                 </select>
  26.                                 <button class="btn btn-primary" id="button">Change</button>
  27.                         </div>
  28.                         <br /><br />
  29.                         <div id="result" style="border:1px solid #000; padding:10px;">
  30.                                 Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.
  31.                         </div>
  32.                 </div>
  33.         </div>
  34. <script src="js/script.js"></script>   
  35. </body>
  36. </html>

Creating the Script

This code contains the script of the application. This code will change the font of the web when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. document.getElementById('button').addEventListener("click", changeFont);
  2.  
  3. function changeFont(){
  4.         var font=document.getElementById('font').value;
  5.        
  6.         document.getElementById('result').style.fontFamily=font;
  7. }      
There you have it we successfully created a Simple Dynamic Change Font using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment