Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Although retrieving content from the page is pretty cool in and of itself, typically that's not enough. Usually you need ways to manipulate the page content once you have it. And luckily jQuery gives us some really good ways of doing that and that's what we're going to examine in this section. Once you've used the selectors and filters to get the content out of the webpage, typically you're going to want to do something with it. In addition to that, there were times when you're going to want to create new content to add it into the page dynamically.
For this purpose you use jQuery's content manipulation functions. As you're about to see, there are functions for creating and copying and deleting and moving content around as well as wrapping page content inside other content. In addition to manipulating content, jQuery provides some really good cross-browser support for working with CSS information including positioning, and sizing, and that's what we're going to take a look at. Let's look at getting, creating, and setting content. So to create new HTML content, you basically just pass a string containing the new HTML to the jQuery function, and put something like this.
The result of this statement will be a new jQuery object, a new wrapped element, a result set if you will, that contains the HTML that you passed in as the string there. And since it's just a string, it also works really well if you want to pass in a variable as well. So if you can make it a variable, call it My String, pass it into the header and that will works fine. In addition to this method, you can use jQuery's html() and text() methods to get and set content on elements.
And if you look at the table here you'll see what these methods do. So the html() method with no arguments basically returns the current HTML content of the first matched element that comes back from the selector's result set that you are calling this function on. However, if you pass it some content, then that sets the HTML content of every single matched element in that set. Similarly, the text() functions work pretty much the same way. You call it with no arguments and that means get the text content of the first matched element.
Otherwise if you pass it some text, it will set the text content for all the matched elements. The main difference between text and HTML is that if you pass HTML code to the HTML function, it will actually create real HTML for you. If you try to pass a string into the text function, however, that contains HTML, the text function will automatically escape it for you. It will change angle brackets into text representations. It's not going to actually create HTML code. Let's jump over to the code and take a look at some of this in action.
Bring it to Design View. You can see this is very similar to the file that we've been using. We have some paragraphs, we've got some list items. So we are going to go back into the Source. What we're going to do now is test out the HTML function to see what it does. So we're going to write jQuery here and let's pass in a selector that gets us that unordered list. And let's get the HTML for it and let's display that in an alert. Okay.
Run this in the browser, and you can see that the alert is displaying the HTML content that is inside of that UL. So let's go back to the code. Let's get a little bit more fancy. Now, let's change the HTML of that list and see what we're going to comment this one out. Let's write something like this, pass in the list, and this time we're actually going to set the HTML. We'll set the HTML to just some new list item. This is a new list item.
So now, when we run this, you're going to see all the list items that are currently there. They're going to be blown away because we're changing the HTML that's going inside the UL tag. So let's go back to the browser. So let's refresh and you can see that all those list items were replaced by the new one. Let's keep on going. Go back to the code editor. So here we are. Let's comment that one out. All right. Let's try something else. Let's try creating some new content. So I am going to write var newItem = and we're going to pass a string to the jQuery object and we'll make a new paragraph.
I will say this is a new paragraph, and we will close that off. So now we've created the new paragraph. Let's insert it somewhere. So let's get a reference to paragraph 2 right here and let's say okay, that HTML is now going to be set to the new item's HTML. Let's run that in the browser and see what happens. I am going to refresh, and you can see that the contents of that paragraph have now been changed.
Let's try one more thing. This time we'll use the Text function. Comment these guys. Let's try one more thing. Let's do something like this. We'll say let's get the last paragraph and we'll set the text to be, "This is the last paragraph." Let's try running that. We'll save, go over to the browser. Okay, let's refresh. So you can see how you can create and get and set content using a couple of very basic jQuery functions.
So let's move onto our next lesson.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96787 Viewers
56 Video lessons · 110486 Viewers
71 Video lessons · 79324 Viewers
131 Video lessons · 38047 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.