JavaScript – Underline Text in HTML Element

To underline text in HTML Element using JavaScript, get reference to the HTML Element element, and assign property with value of "underline".

In the following example, we will underline the text in HTML Element with id "myElement" in JavaScript, using property.


<!DOCTYPE html>
<html lang="en">
        #myElement {
            border:1px solid #CCC;
    <meta charset="utf-8">
    <h2>Underline text in HTML Element using JavaScript</h2>
    <div id="myElement">Hello World!</div>
    <button type="button" onclick="changeStyle()">Click Me</button>
    function changeStyle(){
        var element = document.getElementById("myElement"); = "underline";
Try Online


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