Sunday, November 23, 2025
HomeLanguagesJavascriptJavaScript Get the text of a span element

JavaScript Get the text of a span element

In this article, we are given an HTML document and the task is to get the text of an <span> element. There are two methods used to get the span elements which are discussed below:

HTML DOM textContent Property: This property set/return the text content of the defined node and all its descendants. By setting the textContent property, the child nodes are removed and replaced by a single Text node having the specified string. 

Syntax:

  • Return the text content of a node.
node.textContent
  • Set the text content of a node.
node.textContent = text

HTML DOM innerText Property: This property set/return the text content of the defined node and all its descendants. By setting the innerText property, any child nodes are removed and replaced by a single Text node having the specified string. 

Syntax:

  • Return the text content of a node.
node.innerText
  • Set the text content of a node.
node.innerText = text

Example 1: This example gets the content by using textContent property. 

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        JavaScript Get the text of a span element
    </title>
</head>
 
<body>
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
 
    <span id="GFG_Span" style="font-size: 15px;
                               font-weight: bold;">
        This is text of Span element.
    </span>
 
    <br><br>
 
    <button onclick="gfg_Run()">
        Click here
    </button>
 
    <p id="GFG_DOWN" style="color:green;
                            font-size: 20px;
                            font-weight: bold;">
    </p>
 
    <script>
        let span = document.getElementById("GFG_Span");
        let el_down = document.getElementById("GFG_DOWN");
 
        function gfg_Run() {
            el_down.innerHTML = span.textContent;
        }       
    </script>
</body>
</html>


Output:

JavaScript Get the text of a span element

JavaScript Get the text of a span element

Example 2: This example gets the content by using innerText property. 

html




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        JavaScript Get the text of a span element
    </title>
</head>
 
<body>
    <h1 style="color:green;">
        neveropen
    </h1>
 
    <span id="GFG_Span" style="font-size: 15px;
                               font-weight: bold;">
        This is text of Span element.
    </span>
 
    <br><br>
 
    <button onclick="gfg_Run()">
        Click here
    </button>
 
    <p id="GFG_DOWN" style="color:green;
                            font-size: 20px;
                            font-weight: bold;">
    </p>
 
    <script>
        let span = document.getElementById("GFG_Span");
        let el_down = document.getElementById("GFG_DOWN");
 
        function gfg_Run() {
            el_down.innerHTML = span.innerText;
        }       
    </script>
</body>
</html>


Output:

JavaScript Get the text of a span element

JavaScript Get the text of a span element

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS