• 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 / Using JavaScript to Change Text of Element

Using JavaScript to Change Text of Element

December 2, 2021 Leave a Comment

To change the text in a div using JavaScript, we can use the textContent property:

document.getElementById("div1").textContent="newContent";

If your div will contain html content, then you should use the innerHTML property.

document.getElementById("div1").innerHTML = "<span>Paragraph changed!</span>";

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 use the textContent property to do this with the following JavaScript code.

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

We can also use the innerHTML property to change the content of a div.

document.getElementById("div1").innerHTML = "Today is <span>December</span> 3rd.";

While the textContent property is very simple, the innerHTML property gives us the ability to insert html into our div, which gives us more options when styling the content.

Changing the Text in a Div Using JavaScript with a Click

To change the text of a div using JavaScript, we can combine the textContent property with a click event.

Let’s say we have the following HTML, and we want to give the user the ability to change the text of the div from “November” to “December”.

<div>
  <div id="div1">Today is November 3rd.</div>
  <div id="click-me" onclick="clickFunction()">Update text</div>
</div>

We can utilize both the onclick event and the textContent property to change the text from “November” to “December”.

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

function clickFunction() {
  var currDiv = document.getElementById("div1");
  currDiv.textContent = "Today is December 3rd.";
}

The final code and output for this example of how to change the text of a div using JavaScript is below:

Code Output:

Today is November 3rd.

Update text

Full Code:

<div>
  <div id="div1">Today is November 3rd.</div>
  <div id="click-me" onclick="clickFunction()">Update text</div>
</div>

<script>

function clickFunction() {
  var currDiv = document.getElementById("div1");
  currDiv.textContent = "Today is December 3rd.";
}

</script>

Using the innerHTML Property to Change Text in a Div

We can use the innerHTML property to change the html and text of a div.

Let’s say we have the following code and we want to give the user the ability to add HTML content to the div.

<div>
  <div id="div2">This is original text.</div>
  <div class="click-me" onclick="clickFunction2()">Make bold</div>
</div>

If we want to make the text “content” bold, we can do this easily utilizing both the onclick event and the innerHTML property.

Below is the JavaScript code which will allow the user to change the content in the div.

function clickFunction() {
  var currDiv = document.getElementById("div2");
  currDiv.innerHTML = "<b>This text is now bold.</b>";
}

The final code and output for this example are below:

Code Output:

This is original text.

Make bold

Full Code:

<div>
  <div id="div2">This is original text.</div>
  <div class="click-me" onclick="clickFunction2()">Make bold</div>
</div>

<script>

function clickFunction2() {
  var currDiv = document.getElementById("div2");
  currDiv.innerHTML = "<b>This text is now bold.</b>";
}

</script>

Hopefully this article has been useful for you to understand how to change text in a div using JavaScript.

Other Articles You'll Also Like:

  • 1.  JavaScript tan – Find Tangent of Number in Radians Using Math.tan()
  • 2.  How to Use JavaScript to Change Text in Span
  • 3.  Using Javascript to Add Class to Element
  • 4.  How to Change the Id of an Element in JavaScript
  • 5.  Remove Empty Strings from an Array in JavaScript
  • 6.  Reverse a String in JavaScript
  • 7.  Using the getElementsByClassName Method in JavaScript
  • 8.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 9.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 10.  Using JavaScript to Get the Width of an Element

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