
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to change font style using drop-down list in JavaScript?
We can use the style fontFamily property to change font style using drop-down list in JavaScript. To make the font change effective using the drop-down list, we can use onchange event when an option is selected in the drop down list.
Before we move further, let us know in detail about the drop-down list. A drop-down list is a switchable menu which enables the user to select the one item from a various option. These options can be defined using the HTML code, which are connected to a function. These functions are triggered and starts operating whenever you click or select this option.
Syntax
The following is the syntax for the fontFamily CSS property −
element.style.fontFamily = font
By using the fontFamily CSS property we can format the font of the text, option tag is used to store the font values and the syntax of option tag is as follows −
<option value="value">
Whenever the form gets submitted, the value attributes will specify the value. After selecting the sent value, we use the above-given syntax to specify the value and the font style. Changefontstyle() is used in JavaScript for changing the font style of a text, and Times Roman is set as default in the font style tab.
Steps
The following steps should be followed −
Step 1 − Create a drop-down list in HTML with a selection of font styles. Each option in the list should have a value corresponding to the font style you want to apply.
Step 2 − Select the element you want to apply the font style to using JavaScript.
Step 3 − Add an changeFontStyle to the drop-down list that listens for changes to the selected option.
Step 4 − Add an event listener to the drop-down list that listens for changes to the selected option.
Step 5 − When the event listener is triggered, from the drop-down list choose the font style which is selected.
Step 6 − Use JavaScript to set the font style of the element to the selected font style.
Step 7 − The style.fontFamily is the HTML DOM CSS property which is used to set or change the font family of the text.
Step 8 − If desired, add additional code to handle any other logic or functionality you want to implement.
Example
In these example, we are going to see how can we change the font style of the text using the dropdown list using the JavaScript.
<html> <body> <div id="content"> <h2 style="color:blue;"> Font Style Changes After Selecting the Dropdown List </h2> </div> <select id="input" class="input" onchange="changingFontStyle (this);"> <option value="Times New Roman" elected="selected"> Times New Roman </option> <option value="Arial">Arial</option> <option value="fantasy">Fantasy</option> <option value="cursive">cursive</option> </select> <script> var changingFontStyle = function (fontstyle) { document.getElementById("content").style.fontFamily = fontstyle.value; } </script> </body> </html>
Example
Let us see another example where we use event listener to the dropdown list for changing the font style of the text for the selected options.
<html> <head> <style> .Arial{ font-family: Arial; } .Algerian{ font-family: Algerian; } .Berlin-sans-fb{ font-family: 'Berlin sans fb'; } .Times-new-roman{ font-family: 'Times New Roman'; } </style> </head> <body> <div> <h3>Style of the Text Changes with the drop down</h3> </div> <div id="output-text"> Hi Guys, Welcome to Javascript Program </div> <div> <select id="input-font" class="input" onchange="changingFont (this);"> <option value="Times-new-roman" selected="selected">Times New Roman</option> <option value="Arial">Arial</option> <option value="Algerian">Algerian</option> <option value="Berlin-sans-fb">Berlin Sans FB</option> <option value="bold" class="bolder">Tebal</option> </select> </div> <script> var changingFont = function(fontstyle) { console.log(fontstyle.value) document.getElementById("output-text").className = "text-center " + fontstyle.value; } </script> </body> </html>
In this article, we have demonstrated how can we change the font-style using dropdown list along with examples. We have seen the examples here, where the fontFamily CSS property and changeFontStyle will be used to change or set the font style whenever we select the options from the dropdown list.
- Related Articles
- How to change the font style of IText using FabricJS?
- How to change the font style of Text using FabricJS?
- How to change the font style of a Textbox using FabricJS?
- How to define a drop-down list in HTML5?
- How to disable the drop-down list in HTML5?
- How to add color to a drop-down list in Excel?
- How to change font-weight of a text using JavaScript?
- How to get all options in a drop-down list by Selenium WebDriver using C#?
- How to create a group of related options in a drop-down list using HTML
- How to get selected text from a drop-down list (select box) using jQuery?
- How to include an option in a drop-down list in HTML?
- How to change the font color of a text using JavaScript?
- How to change the font size of a text using JavaScript?
- How to change the font-weight of a text using JavaScript?
- How to Add a Blank Option to a Drop-Down List in Excel?
