• 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 / Set Text with the textContent Property in JavaScript

Set Text with the textContent Property in JavaScript

April 25, 2022 Leave a Comment

We can use the textContent property in JavaScript to set the text of an HTML element.

document.getElementById("div1").textContent = "Some Text";

Let’s say we have the following HTML:

<div id="div1"></div>

If we want to add some text to the div, #div1, we can use the textContent property to do this with the following JavaScript code.

document.getElementById("div1").textContent = "This is new text we will add to #div1";

This would update the HTML to look like this:

<div id="div1">This is new text we will add to #div1</div>

We can also use the textContent property to change text already in an HTML div.

Let’s say we have the following HTML:

<div id="div1">Today is November 3rd.</div>

If we want to change the text inside the div from “Today is November 3rd.” to “Today is December 3rd.”, we can again use the textContent property to do this with the following JavaScript code.

document.getElementById("div1").textContent = "Today is December 3rd.";

Let’s take a look at the textContent property in action with an example below.

Using the textContent property in JavaScript to set the text of a Paragraph

In this example, we will have an empty paragraph. We will let the user input any text they would like. We will then update the empty paragraph with this text using the textContent property.

Here is our simple HTML setup:

<div id="div1">
  <p id="div-to-be-updated"></p>
  <input id="userInput" type="text">
  <div id="click-me" onclick="setText()">Set/Update text</div>
</div>

We will get the user input using the value property.

We can then utilize both the onclick event and the textContent property to set and change the text of paragraph with id #div-to-be-updated with the user input. The user can change the text as many times as they want.

Below is the JavaScript code which will allow the user to be able to set/update the text in the paragraph:

function setText() {
  //Get the user input
  var userInput = document.getElementById("userInput").value;
  
  //Set the text of the paragraph to be what the user entered
  document.getElementById("div-to-be-updated").textContent = userInput;
};

The final code and output for this example of how to set the text of a paragraph using the textContent property in JavaScript is below:

Code Output:

Set/Update text

Full Code:

<div id="div1">
  <p id="div-to-be-updated"></p>
  <input id="userInput" type="text">
  <div id="click-me" onclick="setText()">Set/Update text</div>
</div>

<script>

function setText() {
  var userInput = document.getElementById("userInput").value;
  document.getElementById("div-to-be-updated").textContent = userInput;
};

</script>

Hopefully this article has been useful for you to understand how to use the textContent property in JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 2.  Using JavaScript to Check if Variable is a String
  • 3.  JavaScript Random Boolean – How to Generate Random Boolean Values
  • 4.  Using JavaScript to Stop a Timer
  • 5.  Using JavaScript to Remove the First Character From a String
  • 6.  Using JavaScript to Scroll to Bottom of Div
  • 7.  How to Get the Cursor Position in JavaScript
  • 8.  JavaScript indexOf – Get the position of a value within a string
  • 9.  charAt() JavaScript – Getting the Index Position of a Character
  • 10.  Using JavaScript to Get the URL Path

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