Complete 2024 Web Development Bootcamp

Dr. Angela Yu

Back to jQuery Index Page


jQuery & Text
Intro

To understand how to manipulate text with jQuery, let's start with the basics. Everything in jQuery that relates to text is done with, that's right, you guessed it. The text method.

So for starters, how would you use jQuery to see any text that an element might already contain. Well, with jQuery, it's easy. All you have to do is call an empty text method.

Let's say we have the following HTML element:

  • <h1>I am a Heading Tag</h1>
and we make the following jQuery:
  • $("h1") . text();
it would return: I am a Heading Tag.

And if we wanted to change the text, all we have to do is supply some new text for the text method:

  • $("h1") . text("This is the new text");
and it will update our <h1> to reflect the new text.




jQuery innerHTML

With vanilla JavaScript we had a method called innerHTML which allows us to add or change the HTML coding within an element. We can do the same with the jQuery html method, like so:

  • $("h1") . html("<b>Some text here.</b>");
and this would result in bold text for our <h1>, Some text here. So we've added some "inner" html code to the element.


Back to Top