JavaScript – Change Text in HTML Element to Italic

To change text in HTML Element to italic using JavaScript, get reference to this HTML Element element, and assign element.style.fontStyle property with value of "italic".

In the following example, we will make the text italic for HTML Element with id "myElement", in JavaScript, using element.style.fontStyle property.

example.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #myElement {
            border:1px solid #CCC;
            width:100px;
            height:100px;
        }
    </style>
    <meta charset="utf-8">
</head> 
<body>
    <h2>Make Text in HTML Element Italic using JavaScript</h2>
    <div id="myElement">Hello World!</div>
    <br>
    <button type="button" onclick="changeStyle()">Click Me</button>
    <script>
    function changeStyle(){
        var element = document.getElementById("myElement");
        element.style.fontStyle = "italic";
    }
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to change text in a HTML Element to italic using JavaScript.