JavaScript - Simple Dynamic Change Font
Submitted by razormist on Friday, May 3, 2019 - 14:56.
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 .
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-8">
- <div class="form-inline">
- <select class="form-control" id="font">
- </select>
- </div>
- <div id="result" style="border:1px solid #000; padding:10px;">
- 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.
- </div>
- </div>
- </div>
- </body>
- </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.- document.getElementById('button').addEventListener("click", changeFont);
- function changeFont(){
- var font=document.getElementById('font').value;
- document.getElementById('result').style.fontFamily=font;
- }
Add new comment
- 39 views