Open In App

How to make a text italic using JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to make a text italic using javascript. In HTML we use <i> tag to make text italic we can do the same in JavaScript by using italics() method. This method returns a string embedded in an <i> element.

Syntax:

str.italics()

Parameter: This method doesn’t take any parameters.

Return Value: This method returns a String that is embedded in an <i> element.

Example:

HTML




<!DOCTYPE html>
<html>
  
<body>
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <div id="text">Normal</div>
      
    <button onclick="myFunction()">
        Click Me
    </button>
  
    <script type="text/javascript">
        var str = document.getElementById("text");
        var strItalic = new String("Italic");
        function myFunction() {
  
            // Toggling between normal and italic
            if (str.innerHTML == "Normal") {
  
                // Using italics() to italicize
                str.innerHTML = strItalic.italics();
            }
            else {
                str.innerHTML = "Normal";
            }
        }   
    </script>
</body>
  
</html>


Output:


Last Updated : 31 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads