Code has been added to clipboard!

jQuery .text(): Get or Set Text for HTML Elements

Reading time 1 min
Published Jan 24, 2018
Updated Sep 27, 2019

jQuery text: Main Tips

  • jQuery .text() returns the text inside the selected elements and their descendants.
  • You can also make jQuery change text inside each matched element to the specified content.
  • Text is always defined in a string.

Using .text()

The .text() method sets or returns the text inside the specified elements. Differently from .html(), jQuery .text() is applicable to both XML and HTML documents.

In the example below, the function sets text to a <h1> element:

Example
$("button").click(() => {
    $("h1").text("Hello world!");
});

When no parameters are defined, .text() jQuery retrieves the text inside elements:

$(selector).text();

Follow this syntax to jQuery change text:

$(selector).text(content);

To make jQuery set text, a function can also be added to retun the text needed:

$(selector).text(function);

In this case, the function receives these arguments:

  • index - the index of the currently selected element in the set of matched elements.
  • currentContent - the original text value of elements.

Tip: do not apply .text jQuery to form inputs or scripts. Use .val() instead.

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX