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:Sample Text inside a p element
InnerText
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:Sample Text inside a p element
InnerHTML
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(/<br>/gi,"n").replace(/(<([^>]+)>)/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:
// 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(): para1para2HTML/JS Code to test all of them out and pick one!
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);
}
jQuery4u - This is a division element that contains some red text.
jQuery4u - Get the contents of the division element!
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.
Frequently Asked Questions (FAQs) about jQuery Set InnerText
What is the difference between innerText and innerHTML in jQuery?
In jQuery, both innerText and innerHTML are used to manipulate the content of an HTML element. However, they work differently. innerHTML is used to get or set the HTML content of an element. It will parse the HTML string and render it as HTML. On the other hand, innerText is used to get or set the text content of an element. It will not parse the HTML string, instead, it will display the HTML tags as plain text.
How can I use jQuery to set the innerText of an element?
To set the innerText of an element in jQuery, you can use the .text() method. This method sets the text content of the selected elements. Here is an example:$("p").text("Hello, World!");
In this example, the text content of all paragraph elements will be set to “Hello, World!”.
Can I use jQuery to get the innerText of an element?
Yes, you can use the .text() method in jQuery to get the innerText of an element. When the .text() method is used without any arguments, it returns the combined text content of each element in the set of matched elements. Here is an example:var text = $("p").text();
In this example, the variable text will hold the combined text content of all paragraph elements.
Is there a way to append text to an existing element using jQuery?
Yes, you can append text to an existing element using the .append() method in jQuery. This method inserts specified content at the end of the selected elements. Here is an example:$("p").append(" Additional text.");
In this example, ” Additional text.” will be appended to the end of all paragraph elements.
How can I replace the innerText of an element with new text in jQuery?
To replace the innerText of an element with new text in jQuery, you can use the .text() method. This method replaces the text content of the selected elements with the specified text. Here is an example:$("p").text("New text");
In this example, the text content of all paragraph elements will be replaced with “New text”.
Can I use jQuery to set the innerText of multiple elements at once?
Yes, you can use jQuery to set the innerText of multiple elements at once. When you use the .text() method, it sets the text content for all elements in the set of matched elements. Here is an example:$("p").text("Same text for all paragraphs");
In this example, the text content of all paragraph elements will be set to “Same text for all paragraphs”.
How can I use jQuery to set the innerText of an element to an empty string?
To set the innerText of an element to an empty string in jQuery, you can use the .text() method with an empty string as the argument. Here is an example:$("p").text("");
In this example, the text content of all paragraph elements will be set to an empty string, effectively clearing the text content.
Can I use jQuery to set the innerText of an element based on its current text?
Yes, you can use jQuery to set the innerText of an element based on its current text. The .text() method can accept a function as its argument. This function should return the new text based on the current text. Here is an example:$("p").text(function(index, currentText) {
return "New text: " + currentText;
});
In this example, the text content of all paragraph elements will be set to “New text: ” followed by their current text.
Is it possible to set the innerText of an element to include HTML tags using jQuery?
No, if you use the .text() method to set the innerText of an element, any HTML tags included in the text will be displayed as plain text. If you want to include HTML tags in the content, you should use the .html() method instead.
Can I use jQuery to set the innerText of an element to a number?
Yes, you can use jQuery to set the innerText of an element to a number. The .text() method converts the number to a string and sets it as the text content of the selected elements. Here is an example:$("p").text(123);
In this example, the text content of all paragraph elements will be set to “123”.
![Sam Deering](https://uploads.sitepoint.com/wp-content/uploads/2018/10/1539663963Sam-Deering-150x150.jpg)
Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.