jQuery set innerText(), innerHTML(), textContent()

Sam Deering

jQuery can be used in conjunction with plain ole JavaScript to change the text of a html element and set it to contain new content which could be text, images, code or anything you want. Here we will look in detail at the following JS functions and show you how, when and where you might use them.

This post covers the following jQuery functions: innerText(), innerHTML(), textContent()

jQuery innerText() function

Syntax: document.elementID.innerText = value

Functionality: JavaScript read and write property that specifies the text between the element opening and closing tags.

Behaviour: Includes line breaks.

Browser Compatiablity: innerText() works in all browsers that we have tested on.

Basic Example:

<html>
<body>
<p id="myP">Sample Text inside a <b>p</b> element</p>
<button onclick="alert(myP.innerText);">InnerText</button>
</body>
</html>

JavaScript innerHTML() function

Syntax:

document.getElementById(“elementID”).innerHTML = value
document.all.elementID.innerHTML = value // IE only

Functionality: Native JavaScript function to change the html within a page element.

Behaviour: It strips out line breaks.

Browser Compatiablity: innerHTML is supported in all browsers.

Basic Example:

<html>
<body>
<p id="myP">Sample Text inside a <b>p</b> element</p>
<button onclick="alert(myP.innerHTML);">InnerHTML</button>
</body>
</html>

Advanced Example: Regular Expression replacing
tags by n makes sure that it works like innerText, and the final replace() is a regular expression that removes all HTML tags.

var message = div.innerHTML.replace(/&amp;lt;br&amp;gt;/gi,"n").replace(/(&amp;lt;([^&amp;gt;]+)&amp;gt;)/gi, "")

jQuery textContent() Function

Syntax:

var text = element.textContent;
element.textContent = "i love jQuery (4u :P )";

Functionality: jQuery FF Function to change the text of a page element.

Behaviour: It strips out line breaks.

Browser Compatiablity: Firefox has its own property called textContent which is supported by Chrome and Opera but IE does not support it!

Basic Example(s):

Given the following HTML fragment:

This is some text
// Get the text content:
var text = document.getElementById("divA").textContent;
// Set the text content:
document.getElementById("divA").textContent = "This is some text";

The linebreak problem

innerText() shows “para1? and “para2? with a line break in-between but textcontent() does not:

//IE/innerText():
para1
para2
//FF/textcontent():
para1para2

HTML/JS Code to test all of them out and pick one!

<head>
    <script type="text/javascript">
        function GetContent () {
            var elem = document.getElementById ("myDiv");
            var message = "";
            if (elem.outerHTML !== undefined) {
                message += "outer HTML : " + elem.outerHTML;
            }
            if (elem.outerText !== undefined) {
                message += "nouter text : " + elem.outerText;
            }

            message += "ninner HTML : " + elem.innerHTML;

            if (elem.textContent === undefined) {
                message += "ninner text : " + elem.innerText;
            }
            else {
                message += "ninner text : " + elem.textContent;
            }

            alert (message);
        }
    </script>
</head>
<body>
    <div id="myDiv">
        jQuery4u - This is a division element that contains some <span style="color: red">red text</span>.
    </div>
    <br /><br />
    <button onclick="GetContent ();"> jQuery4u - Get the contents of the division element!</button>
</body>

Other jQuery functions that can be used to change page elements: innertext.replace, innerHTML, innerText, textContent, html(), text(), div.innerHTML.replace, document.body.innerText, $.fn.innerText, div:contains, document.getElementById(id).innerText.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • yolo

    6
    3
    66