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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / JavaScript / How to Use JavaScript to Change Text in Span

How to Use JavaScript to Change Text in Span

December 10, 2021 Leave a Comment

There are a couple of ways that we can change the text in a span using JavaScript. The first way we can do this is with the innerHTML property:


document.getElementById("div1").innerHTML = "Changing <span>this text</span> in #div1";

Let’s say we have the following HTML:

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

The innerHTML property gives us the ability to insert HTML into our div and change the HTML that was in there. By doing this we can change the span text that was in there too.

So to change the span text in the div above from “November” to “December” we can use the following code:

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

Which would result in:

Today is December 3rd.

Changing the Text in a Span with JavaScript using the Id and TextContent property

Another way we can change the text of a span using JavaScript is to give the span an id, and target the id with the getElementById method.

Let’s say we have the following HTML:

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

We can give the span an id of “span1”.

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

We can then target the “span1” id and change the text of it using the textContent property. Here is the code:

function clickFunction() {
  var currSpan = document.getElementById("span1");
  currSpan.textContent = "December";
}

Which would result in:

Today is December 3rd.

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Remove Leading Zeros
  • 2.  Using JavaScript to Remove an Element From the DOM
  • 3.  Set Checkbox as Checked in JavaScript
  • 4.  Creating a JavaScript Function to Subtract Two Numbers
  • 5.  Using JavaScript to Convert String to Boolean
  • 6.  parseInt() JavaScript – Convert String to Integer with parseInt() method
  • 7.  How to Check if a Number is Even or Odd in JavaScript
  • 8.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 9.  JavaScript atan – Find Arctangent and Inverse Tangent of Number
  • 10.  Using JavaScript to Reverse a Number

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