JavaScript Change the Text of a Span Element
Last Updated :
17 Sep, 2024
Updating the text of HTML elements, such as a <span> in JavaScript allows developers to dynamically alter webpage content. This technique is used for creating interactive web features like displaying user feedback, updating real-time data, or modifying elements based on user actions. Here, we'll explore how to change the text of a <span> element using two primary properties.
Selecting the Span Element
To modify the text of a <span>, you first need to select it using JavaScript. Common methods for selecting elements include:
- getElementById: Selects an element by its unique ID.
- querySelector: Selects the first matching element using a CSS selector.
Properties to Change Text Content
1. textContent Property: This property sets or returns the plain text content of an element and all its child nodes, ignoring HTML tags and styling. It replaces the entire content with the specified text.
Syntax:
// Retrieve text content
let text = element.textContent;
// Set text content
element.textContent = 'New Text';
2. innerText Property: This property sets or returns the visible text content of an element, respecting styling, visibility, and line breaks. It updates the text as it appears to the user.
Syntax:
// Retrieve visible text
let text = element.innerText;
// Set visible text
element.innerText = 'New Text';
There are two properties used to change the content, these are:
Examples to Change Text of a Span Element
Example 1: Using textContent to Change Span Text
In this example, we'll change the content of a <span> using the textContent property. Clicking the button will update the <span> text to "New Span content" and change the <p> content to "Span content changed".
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>
JavaScript Change the text of a span element
</title>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<span id="GFG_Span">
This is text of Span element.
</span>
<br><br>
<button onclick="gfg_Run()">
Change
</button>
<p id="GFG_DOWN"></p>
<script>
let span = document.getElementById("GFG_Span");
let el_down = document.getElementById("GFG_DOWN");
function gfg_Run() {
span.textContent = "New Span content";
el_down.innerHTML = "Span content changed";
}
</script>
</body>
</html>
Output:
JavaScript Change the text of a span elementExample 2: Using innerText to Change Span Text
Here, we'll use the innerText property to update the text of a <span>. Clicking the button will change the <span> text to "New Span content" and update the <p> content.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>
JavaScript Change the
text of a span element
</title>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<span id="GFG_Span">
This is text of Span element.
</span>
<br><br>
<button onclick="gfg_Run()">
Change
</button>
<p id="GFG_DOWN"></p>
<script>
let span = document.getElementById("GFG_Span");
let el_down = document.getElementById("GFG_DOWN");
function gfg_Run() {
span.innerText = "New Span content";
el_down.innerHTML = "Span content changed";
}
</script>
</body>
</html>
Output:
JavaScript Change the text of a span element
Similar Reads
JavaScript Get the text of a span element We are given an HTML document and the task is to get the text of an <span> element. Below are the methods to get the text of a span element using JavaScript:Table of ContentHTML DOM textContent PropertyHTML DOM innerText PropertyHTML DOM textContent PropertyThis property set/return the text co
2 min read
JQuery | Get the text of a span element Given an HTML document and the task is to get the text of a <span> tag using JQuery. Method 1: Using jQuery text() Method: This method is used to set or return the text content of specified elements. If this method is used to return content, it returns the text content of all matched elements
3 min read
Change an Element Class with JavaScript Here are two different ways to change the element class with JavaScript.1. Using .className PropertyIn this approach, we are using document.getElementById() method to get access to the element for which we want to change the class name then we use using .className property on that selected element t
2 min read
How to change style/class of an element using JavaScript ? In this article, we will learn how we can change the style/class of an element. If you want to build a cool website or app then UI plays an important role. We can change, add or remove any CSS property from an HTML element on the occurrence of any event with the help of JavaScript. There are two com
4 min read
JavaScript - How to Change the Content of a <Textarea> ? Here are the various methods to change the content of a textarea using JavaScript.1. Using value PropertyThe value property is the most common method to change or retrieve the content of a <textarea>.HTML<textarea id="myTextarea">Initial content</textarea> <button onclick="chang
2 min read
JavaScript adding a class name to the element In JavaScript, adding a class name to an element allows you to style or manipulate it via CSS or JavaScript. This can be achieved using different methods. Here, we'll explore two common approaches: using the .className property and the .add() method.Below are the different approaches that could be u
3 min read
How to change font-weight of a text using JavaScript ? In this article, we will set the font-weight of a text dynamically using JavaScript. Ti change the font weight dynamically, we use HTML DOM Style fontWeight property. Syntax: object.style.fontWeight = "value" Property Values: normal: The font weight is the default value.lighter: The font weight is t
1 min read
How to change the text of a label using JavaScript ? Changing the text of a label using JavaScript involves selecting the label element and updating its textContent or innerHTML property. This allows you to modify the displayed text dynamically, often in response to user interactions or changes in data, enhancing interactivity and responsiveness.Below
2 min read
How to Change the Color of HTML Element in JavaScript? Changing the text color of an HTML element using JavaScript enhances the user experience by making content more interactive and visually engaging. By accessing and modifying the element's style properties through the DOM, we can dynamically update font colors based on user actions or events. For exa
2 min read
Set the Value of an Input Field in JavaScript We will learn how you can set the value of an input field in JavaScript. We will set user input to the placeholder value of the input field using JavaScript.Below are the approaches to set the value of an input field in JavaScript:Table of ContentBy using the innerHTML propertyBy using setAttribute
2 min read