JavaScript – Underline Text in Paragraph

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

Example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head> 
<body>
    <p id="myPara">Underline Paragraph in JavaScript</p>
    <button type="button" onclick="changeStyle()">Click Me</button>
    <script>
    function changeStyle(){
        var element = document.getElementById("myPara");
        element.style.textDecoration = "underline";
    }
    </script>
</body>
</html>
Try Online

Conclusion

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