• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Adding an Underline with JavaScript

Adding an Underline with JavaScript

May 1, 2022 Leave a Comment

To underline a paragraph using JavaScript, we can simply use the style textDecoration property.

document.getElementById("p1").style.textDecoration = "underline";

Let’s say we have the following HTML:

<div id="div1">
  <p id="p1">We will change this text to be undelined.</p>
</div>

If we want to add an underline to the paragraph, we will use the textDecoration property in the following JavaScript code.

document.getElementById("p1").style.textDecoration = "underline";

If we wanted to change the font size of the paragraph, we could do that easily by targeting the fontSize property.

We can change many styles using JavaScript by targeting the style properties of an element.

Adding an Underline to a Paragraph in JavaScript with a Click

To underline a paragraph using JavaScript, we can target the style textDecoration property with a click event.

Let’s say we have the following HTML and we want to add an underline to the first paragraph only.

<div>
  <p id="p1">The Programming Expert</p>
  <p id="p2">Code Snippets to Help You Solve Your Programming Headaches Fast</p>
  <p id="p3">Find Solutions for Your Programs in Python, PHP, HTML, SAS, JavaScript and more.</p>
  <div id="click-me" onclick="setUnderline()">Underline first text</div>
</div>

We can utilize both the Style textDecoration property of #p1 along with an onclick event to add an underline to the first line of text.

Here is the JavaScript code:

function setUnderline() {
  document.getElementById("p1").style.textDecoration = "underline";
}

The final code and output for this example of how underline a paragraph using JavaScript is below:

Code Output:

The Programming Expert

Code Snippets to Help You Solve Your Programming Headaches Fast

Find Solutions for Your Programs in Python, PHP, HTML, SAS, JavaScript and more.

Underline first text

Full Code:

<div>
  <p id="p1">The Programming Expert</p>
  <p id="p2">Code Snippets to Help You Solve Your Programming Headaches Fast</p>
  <p id="p3">Find Solutions for Your Programs in Python, PHP, HTML, SAS, JavaScript and more.</p>
  <div id="click-me" onclick="setUnderline()">Underline first text</div>
</div>

<script>

function setUnderline() {
  document.getElementById("p1").style.textDecoration = "underline";
}

</script>

Hopefully this article has been useful for you to understand how to add an underline to a paragraph in JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get URL Hash
  • 2.  Check if Checkbox is Checked in JavaScript
  • 3.  Using JavaScript to Check if Number is Divisible by Another Number
  • 4.  JavaScript Trunc with Math.trunc() Method
  • 5.  Creating a JavaScript Function to Divide Two Numbers
  • 6.  Using JavaScript to Check if Variable is a Number
  • 7.  JavaScript atan – Find Arctangent and Inverse Tangent of Number
  • 8.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 9.  JavaScript Random Color – How to Generate a Random Color In JavaScript
  • 10.  Using JavaScript to Remove Leading Zeros

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy