Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this lesson, we are going to see how to use jQuery for wrapping, replacing, and removing content. Up until now, we have seen how to insert and manipulate some content, but now we are going to do a little bit of additional operations. So, in addition to inserting content, jQuery has a bunch of functions for doing some additional operations like wrapping content in the page, replacing it, copying it, removing it, all that great stuff. So here is the list of functions that you use in order to do that. And as you can see there is a half-a- dozen functions just for wrapping the content. So, let's take a look at those. The Wrap functions basically takes matched elements and wraps them inside something else. So, in this case for example the Wrap function will take each one of the elements on the matched set that you are calling the Wrap function on, and place them inside the HTML that you specify here. So, for example if I was calling the Wrap function on a whole bunch of images, and I passed the String, like DIV inside here, then each one of the images will be wrapped inside it's own separate DIV. Similarly, the Wrap Element works pretty much the same way. Each one of the elements inside the matched set that this is being called on, will be wrapped inside the element that I specify here. The wrapAll works pretty similarly although what it does is rather than wrapping each one of the individually matched elements into it's own wrapper. It will take all of the elements inside the matched set and place them in either the HTML that I am specifying here, or the element. And finally, the wrapInner functions will wrap the inside trial contents of each matched element, including text within either HTML or the DOM element that specify here assuming that the DOM element that I am passing is allowed to have children inside of it. So, for example Images can't have child elements inside them. So that wouldn't work. If I call wrapInner on an ul tag and I had a whole bunch of allies, then all those allies will be wrapped inside whatever I passed in as this HTML or this HTML or this element. So that's wrapping you can also replace. So the replaceWith function, you give it some content, and it will replace all of the matched elements in the result set with whatever content you specify here. And content either be HTML or a series of DOM elements. And the replaceAll basically replaces the elements that are matched by the selector with the matched elements. So, this selector here is going to be used to find which should be replaced by the all the elements that are being called on. And the last couple of functions are some pretty convenient functions, Empty basically removes all the child nodes from all the elements that are in the set, and you are calling it on. Remove just removes all the matched elements from the DOM. So, what you do is you write your jQuery selector, then you Call, Remove and then all those elements taken out the document. Then the Clone function makes copies. So, this Clone function clones the matched DOM elements and then selects all the clones. This version here which takes a Boolean argument, if you pass True then it will clone not only the elements, but it will also copy all the event handlers, whereas this one will not. Okay so that's a trip down that function set. Let's take a look at some examples in the code. I am going to open up my wrapping_start file right here. The same document we have been working on. The list is here paragraphs, and so on. Let's do a couple of experiments. So let's wrap all the paragraph tabs, and let's wrap each one into a DIV who's style has been set to color:red. So all the text inside those DIVs should be red. We need to put that in a tag. So, let's do that and then let's close the DIV off. Okay, so let's try that out, go to the browser and see what happens. So, you can see that the paragraphs two and four. The ones that did not have classes set on them, have now changed to Red text. And that's because these guys do not have any style set, so their text color is being affected by the DIV that it was wrapped into. Let's try another example. Let's go back to the code. Let's try wrapping all of the paragraphs in a DIV whose style is let's just see, border: 3px solid red, save. And go back to the browser and let's refresh. You can see that the wrapAll functions has taken all of the paragraphs, and wrapped them inside a DIV whose border is now red, and three pixels wide. Okay so far so good. Let's go back to the code, let's comment this guys out. All right, now let's try the remove the Empty function. So, I am going to write a expression here that gets the unordered list and I am going to call Empty, and go back to browser and refresh, and you can see that the list items have now all then emptied out from the unordered list. Okay, so that should give you a pretty good idea of how these functions work. Now you have seen how to create content, you've seen how to insert it, and now you're going to go have to do some other manipulations on it like wrapping and replacing and removing.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101646 Viewers
61 Video lessons · 88404 Viewers
71 Video lessons · 72239 Viewers
56 Video lessons · 103980 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.
Your file was successfully uploaded.