jQuery: add and remove DOM elements - Beau teaches JavaScript
Skills:
JavaScript Fundamentals90%
Key Takeaways
Adds and removes DOM elements using jQuery
Full Transcript
In this video, I will talk about how to add and remove elements in jQuery. If you need help adding jQuery to your project, check out my getting started with jQuery video. So, the append and prepend methods add new elements as child elements to the target, and the after and before methods add new elements as sibling elements. So, right now, I just have this code up here, and I do have some styling down here. And one thing I do have some styling for a child element and a sibling element, so it's more obvious what where the child and sibling elements are. So, let's add some jQuery here. So, first I'm going to select the target and then do append. Okay, you can see I appended a div with the class of child, and it just says append inside. So, I'm going to run that and see what happens. Remember, append and prepend add elements as child elements to the target. So, that just means it's going to be inside the target, and it's going to be a child inside the target. And so, I'll show you the same thing with prepend. Just going to copy and paste this. And if we run that, we can see prepend is inside the target. So, now we're going to do before and after. And as you can see, you can chain these methods together pretty easily. I'm going to do before, but now I'm going to do the class as sibling, and we'll put before. Run that. And now it's going to be before the item. And then after is pretty much just like that. You can also add more than one element at a time. So, if I just go up to the append, I'm going to do a comma, and I'm going to add another element. This time, I'm going to add a paragraph element. So, we appended this child div, and then we appended another paragraph down here. And you can also append, prepend, do before and after to many things at once. So, for instance, I can select all the paragraph tags. And if I run that, you can see the word lots was appended to the end of each paragraph tag, and it's in bold. And to remove elements and content, there mainly two jQuery methods. We have remove, which removes the selected element and its child elements, and empty, which removes the child elements from the selected element. So, let me show you some examples of that. So, here I'm selecting the target, and I'm calling the remove method. If I just run that, the entire target is going to be gone. Now, we just have the before and after divs that we added before. However, if we move this remove and put it above when we appended and prepended things, uh now it's not going to be able to append and prepend things. So, so now it's not going to show the before and after because there's no target to add things before and after. Okay, let's move that back down. We can also remove all the paragraph tags. So, now we just have the child and sibling divs left. And then the other thing was empty. We're going to go back to selecting the target, and I'm going to run the empty method. So, now the target div is still there, but everything inside the target div is gone. I'm going to show you one final thing with the remove method. We're going to go back to selecting all the paragraphs, remove, and now I can enter a parameter to filter what's being removed. So, if I add a class to this paragraph tag. Now, I can actually remove it based on that class. So, now, instead of it removing all the paragraphs, I'm just going to remove the paragraph that has the filter class. So, if I run that, you can see the filter this paragraph was this div is a target for all sorts of things and it's not in here anymore. Okay, well, thanks for watching. My name is Beau Carnes. Don't forget to subscribe and remember, use your code for good.
Original Description
Learn how to use jQuery to add and remove DOM elements with append, prepend, after, and before.
💻 Code: https://codepen.io/beaucarnes/pen/zdGMEz
🐦 Beau Carnes on Twitter: https://twitter.com/carnesbeau
-
Learn to code for free and get a developer job: https://www.freecodecamp.com
Read hundreds of articles on technology: https://medium.freecodecamp.com
And subscribe for new programming videos every day: https://youtube.com/subscription_center?add_user=freecodecamp
❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp
Playlist
Uploads from freeCodeCamp.org · freeCodeCamp.org · 59 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
▶
60
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
Dates - Beau teaches JavaScript
freeCodeCamp.org
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
The Definition of Ready - Agile Software Development
freeCodeCamp.org
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
Working Agreement - Agile Software Development
freeCodeCamp.org
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
Definition of Done - Agile Software Development
freeCodeCamp.org
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
The INVEST approach to product backlog items
freeCodeCamp.org
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
How ProvChart Makes Your Pages Faster, Crawlable, and Still Dynamic
Dev.to · FSCSS tutorial
A CSS Hover-Reveal Pattern for Technical Specs
Dev.to · Richard Lemon
HTML vs JSX — What You Actually Lose When You Switch
Medium · Programming
A modern marketing site without a modern front-end stack
Dev.to · NessFlow
🎓
Tutor Explanation
DeepCamp AI