JavaScript - Simple Dynamic Change Font Color

In this tutorial we will create a Simple Dynamic Change Font Color using JavaScript. This code will dynamically change the font color when the user choose on the select form. The code use onclick() function to initiate a certain method by binding the color id value in order to call the dot notation of the targeted id, to be able to change the font color by adding the color value in the style.color. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

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. <!DOCTYPE html>
  3. <html lang="en">
  4.         <head>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7.         </head>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">JavaScript - Simple Dynamic Change Font Color</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-2"></div>
  18.                 <div class="col-md-8">
  19.                         <center><h2 id="target">SOURCECODESTER</h2></center>
  20.                         <br />
  21.                         <div class="alert alert-info"><center>UPDATE FONT COLOR HERE</center></div>
  22.                         <div class="col-md-2"></div>
  23.                         <div class="col-md-8">
  24.                                 <div class="form-inline">
  25.                                         <select id="color" class="form-control">
  26.                                                 <option value="">Select color</option>
  27.                                                 <option value="orange">Orange</option>
  28.                                                 <option value="green">Green</option>
  29.                                                 <option value="red">Red</option>
  30.                                                 <option value="yellow">Yellow</option>
  31.                                                 <option value="blue">Blue</option>
  32.                                         </select>
  33.                                         <button class="btn btn-warning" onclick="updateColor();">Update</button>
  34.                                 </div> 
  35.                         </div>
  36.                 </div>
  37.         </div>
  38. </body>
  39. <script src="js/script.js"></script>
  40. </html>

Creating the Script

This code contains the script of the application. This code will change the font color 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. function updateColor(){
  2.         var color=document.getElementById('color').value;
  3.        
  4.         if(color != ""){
  5.                 var target = document.getElementById('target');
  6.                
  7.                 target.style.color=color;
  8.         }
  9. }
There you have it we successfully created a Simple Dynamic Change Font Color 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