
jQuery includes multiple functions to manipulate the actual HTML of the document you are working with. For instance, you may want to “inject” some text into a specific element on the page. jQuery makes this, and more, very easy.
Today we’ll be going over a couple of the basic manipulation functions, while next week we’ll go over the more complex functions. The first function we’ll look at is text(), using this function, we can replace the current contents of the selected element, with the specified text. If you don’t specify an attribute, jQuery will return the current contents of the selected element to you.
While you can only use strictly text in the text() function, we can use html() to replace the contents with the specified HTML. Like text(), not specifying an attribute will return the current contents.
Let’s have a look at some examples of these:
[javascript]$("div").text(‘Hello world!’);[/javascript]
[javascript]$("div").html(‘<span style="color:red">Hello world!</span>’);[/javascript]
[javascript]if ($("div").text() == ‘Hello world!’) {
$("div").html(‘<strong>Hello world!</strong>’);
}[/javascript]