jQuery Tutorial #4 - DOM Traversal with jQuery

LearnCode.academy · Beginner ·🌐 Frontend Engineering ·12y ago

Key Takeaways

Uses jQuery for DOM traversal in frontend engineering

Full Transcript

okay so one of the things you're going to see on job descriptions all over the place or on uh job postings is Dom traversal um you're going to see things like able to Traverse the Dom with ease able to bend the Dom to your will um skilled at Dom interaction and what that means is uh you kind of know your way around the document object model which is what the Dom stands for and what the document object model is is when your browser reads your HTML file um it actually turns each one of these elements you've made into an element in the in the browser that JavaScript can talk to that CSS Styles can be applied to um and so kind of when the browser reads all three Technologies it creates the Dom and then it can render it for the viewer to see and so when you interact with JavaScript you're not actually interacting with your HTML code you're interacting with what the browser built from your HTML code you're interacting with the Dom so uh when you see things like that in job descriptions they're looking for can you easily uh find specific and sometimes complicated things out of the Dom and work with them and that's where jQuery really shines uh it is actually primarily a Dom traversal Library it's made to do things that would normally be very complex with JavaScript alone uh it's make it makes them very easy so let's get into Dom traversal with jQuery um I'm actually going to start over here with uh the console because it makes it a little easier we know that you can can go Li and find all the LI elements there you go that found all my li elements on the page uh a cool thing you can do is you can go first and that's going to find your first one on the page and then you can go last and that's going to find the last one on the page which is not actually the last one in my list it's the last one on the entire page uh which is this Li which is inside of another Li um so let's say I can actually go first and I can hide it there you go the first one's hidden I can show it again I can go EQ which will actually get me an index so if I go eq0 that's going to get me the first index which is actually the same as first um but I can actually go eq1 which will give me the second one so the eqs would go 0 1 2 three which is the LI right here three then four five six so I have seven seven Lis on the page eq0 through six so that would kind of be how access those um let's look at children so let's say I wanted to get UL first here's another way you can do it if you want to do it in the selector same thing children will get me all the Lis that are direct children of my element so if I look back over here at my HTML code I have a child Li child Li child Li these Lis down here will not show up in that list because it's only getting my first children so that's kind of a way to help narrow that selection down if I only wanted to hide the children of list that would be how I do that um another one is siblings so if I want to go Li first siblings then that's going to give me all the siblings of this guy but this guy will not be in the list because it's just my siblings so that's those three elements there um so let's actually let's see there's another one then that would be parent Li first. par um and that's going to give me the UL that is my parent and you can actually keep chaining that up so let me go Li eq4 which will be this guy eq4 that's him right there and then eq4 parents is going to give me the sublist which is ul and if I go parent parent then that's going to give me the LI that that's a part and you can just keep going up the chain and that's going to give me the list another thing I can do is go parent parent pre so now I'm going this Li and I'm going parent which is the whole UL I'm going parent again which is this Li and I go preve that's going to go the previous sibling now so that should give me this three right here and there you go yep you have it so now if I go pr. preve now I'm going up to two so I'm going all the way up the chain I'm going to the previous to the previous and now if I go pr. pr. next which makes absolutely no sense why would I go back only to go forward I'm back up to three probably an easier way to do this would go Li first. next which is going to give me the same as eq1 so that's kind of how you start moving around things let me show you a couple use cases of how you could do stuff like this I'm going to go Li on click and I'm going to go this next. hiide so whenever I click I'm just going to hide the next li um so I'm going to click on you and number two's gone and now I'm going to click on you and number four is gone if I click on you nothing happens because number two still exists it's just hidden so if I would actually go next. remove I could actually keep doing this over and over again there you go uh so that's kind of how that works uh another thing that I could do is I could hide all the siblings I'll just remove them why not why make them go away when you can make them gone forever so now whichever one you click on is the one that's going to say you notice this one's blue it's because it has a class of special um I made this simple rule that anything with special gets blue uh so what I could actually do is I could make all my siblings have a class of special whenever you click on something whenever you click on something which so now I didn't become special but all my siblings became special um and what I could actually do is make sure that my class special gets removed if I do have it so that way there you go now whatever I click on all my siblings will get the special class but I won't have the special class um what's another thing you could do I could actually go parent and add the special class to my parent so now when I click on this the parent gets a special class so everybody within it gets it so that's kind of another thing that you can do um I can also go closest I don't think I did closest yet and then closest you have to give it something so I'm going to go. list so it's going to go up the chain till it find something with a class of lists so basically no matter what Li I click on it's going to go all the way up the chain are you a list are you a list oh you're a list and so then I'm going to add the class to that so now I can click here and it goes all the way up add special to the entire thing um let's see I'll give you a couple more examples before I leave you guys for now um you could do filter um so let's say whenever I click on list I'm going to go. list I'm going to go filter uh filters actually yeah so I'll go find I'm going to go find all the liis within my element so whenever I click anywhere on this list um I'm going to find all the UI all the liis within list um and I'm going to remove um I'll do add the class special same thing as I've been doing so this should do just what we've been doing there we go uh but let's say I only actually want to add the class to the first li of each one I can do that guy so only the first elements are going to get it now uh the first element um I could also go filter special and I'll actually do a remove here hope I'm not going too fast for you at this point so whenever I click on anything inside of list this is going to be list because that's what my selector was here I'm going to find any Li in it filtered by special so I'm going to sight I'm going to find all the Lis I'm going to filter them down to where only special exists and I'm going to remove special so now I'm clicking and special's gone of course an easier way to do this would just go find special but then I couldn't teach you about the filter method now could I there you go exact same thing so find is awesome another great thing about find is it's really really fast because it's only looking in this element it's not having to look at my whole page I could get around this by doing this exact same thing here whenever I click remove special right that's easy exact same thing but now it had to search my entire page to find one item with a class of special that's not very efficient JavaScript so I'm going to stay within the element that we're working with I'm going to find special inside of it and then I'm going to remove it um another thing that I can do is I can uh do an action only if um let me do another thing so any I click on I can do an if and alert special so if it's a special one that I clicked on I want to alert special so of course we can do this I mean that's easy you know anytime you click on special alert special of course should do that oh this if is so now whenever I click on special it alerts special awesome okay um uh let's say I want to do other stuff to every other Li say in here other stuff happens um oh like let's say I want to hide it um and if I'm hiding a special one I also want to alert special so I can go if this is special if this is special alert special so now whenever I click on Nei it's going to hide it and that's going to hide it and say haha special so another thing I can do if this is not special alert not special not special and not special so that's kind of how you can do those things um while I'm on this I'm hiding every Li that gets clicked on and look at this when I click on one of these subis over here it hides the entire menu That's because when I click I'm clicking on you and I'm clicking on you because I'm inside of you at the same time so there's a listener for both of these liis um couple ways you could get around this I could just Target sublist um I can only hide ones when I click on the subl list um um sorry sublist UL or Li so now I'm only going to I'm only listening to Lis on the sublist these aren't doing anything okay that's one way around it but say I still wanted to do a bunch of other stuff to all liis so I want to do other stuff to all liis and I only want to hide the LI if it's on the subl list so I could go if this do parent is sublist so if the parent is a sublist then I'm going to hide it uh and if it's not I'm not going to do anything else so now I'm doing other stuff like here let me say I want a console log so now whenever I click on any Li it's going to log down here that I did fix this there we go when I click on any l it's going to say clicked on Li as you notice down there that number keeps going up and when I click on you it's going to also log and it's going to hide so that's kind of how we can do some smarter things like that uh I think that just about covers it I tried to go fast for those of you guys that are a little more comfortable you can get it and be done and some of you guys may want to rewatch this video um but that's about it I hope you enjoyed learning about Dom traversal with jQuery

Original Description

Lots of jobs require you to be good at DOM traversal. This jQuery tutorial focuses on using jQuery as a DOM traversal library - which is where jQuery really shines. The DOM (Document Object Model) is all of the html elements that get generated when your browser reads the html file. DOM traversal is finding elements or groups of elements and manipulating them to change the state of the web page. You may read job posts looking for a "DOM traversal ninja", or someone who's "skilled at advanced DOM traversal" or "Bends the DOM to your will"...that's what we'll be learning in this jQuery tutorial for beginners. Lesson #1: jQuery Tutorial for Beginners https://www.youtube.com/watch?v=hMxGhHNOkCU Lesson #2: Listen to user events and respond with jQuery actions! https://www.youtube.com/watch?v=G-POtu9J-m4 Lesson #3: Clean up the jQuery by putting some data in the HTML https://www.youtube.com/watch?v=Cc3K2jDdKTo Lesson #5: Build a jQuery Panel Widget https://www.youtube.com/watch?v=1nWrIBB_bMA -~-~~-~~~-~~-~- Also watch: "Responsive Design Tutorial - Tips for making web sites look great on any device" https://www.youtube.com/watch?v=fgOO9YUFlGI -~-~~-~~~-~~-~-
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LearnCode.academy · LearnCode.academy · 25 of 60

1 Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
LearnCode.academy
2 Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
LearnCode.academy
3 How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
LearnCode.academy
4 Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
LearnCode.academy
5 Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
LearnCode.academy
6 CSS Tutorial - Web Development Tutorial for Beginners (#5)
CSS Tutorial - Web Development Tutorial for Beginners (#5)
LearnCode.academy
7 Node.js tutorial for beginners - an introduction to Node.js with Express.js
Node.js tutorial for beginners - an introduction to Node.js with Express.js
LearnCode.academy
8 Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
LearnCode.academy
9 Javascript Tutorial - Programming Tutorial for Beginners Pt 1
Javascript Tutorial - Programming Tutorial for Beginners Pt 1
LearnCode.academy
10 Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
LearnCode.academy
11 AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
LearnCode.academy
12 WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
LearnCode.academy
13 YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
LearnCode.academy
14 BOWER! - Streamline Web Workflow with Bower Package Manager
BOWER! - Streamline Web Workflow with Bower Package Manager
LearnCode.academy
15 Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
LearnCode.academy
16 GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
LearnCode.academy
17 GITHUB PULL REQUEST, Branching, Merging & Team Workflow
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
LearnCode.academy
18 Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
LearnCode.academy
19 jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
LearnCode.academy
20 jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
LearnCode.academy
21 jQuery Tutorial #1 - jQuery Tutorial for Beginners
jQuery Tutorial #1 - jQuery Tutorial for Beginners
LearnCode.academy
22 Node.js MongoDB Tutorial using Mongoose
Node.js MongoDB Tutorial using Mongoose
LearnCode.academy
23 Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
LearnCode.academy
24 WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
LearnCode.academy
jQuery Tutorial #4 - DOM Traversal with jQuery
jQuery Tutorial #4 - DOM Traversal with jQuery
LearnCode.academy
26 jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
LearnCode.academy
27 jQuery Tutorial #6 - Building a jQuery Image Slider
jQuery Tutorial #6 - Building a jQuery Image Slider
LearnCode.academy
28 jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
LearnCode.academy
29 jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
LearnCode.academy
30 jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
31 jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
32 How to put your website online - how to FTP to a domain & upload files to a webhost
How to put your website online - how to FTP to a domain & upload files to a webhost
LearnCode.academy
33 Basic Terminal Usage - Cheat Sheet to make the command line EASY
Basic Terminal Usage - Cheat Sheet to make the command line EASY
LearnCode.academy
34 SSH Tutorial - Basic server administration with SSH
SSH Tutorial - Basic server administration with SSH
LearnCode.academy
35 Vagrant Tutorial - Running a VM For Your Local Development Environment
Vagrant Tutorial - Running a VM For Your Local Development Environment
LearnCode.academy
36 Sublime Text Favorite Packages and Workflow
Sublime Text Favorite Packages and Workflow
LearnCode.academy
37 What Makes Javascript Weird...and AWESOME - Pt 1
What Makes Javascript Weird...and AWESOME - Pt 1
LearnCode.academy
38 Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
LearnCode.academy
39 Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
LearnCode.academy
40 FREE REST API - Practice Developing Javascript AJAX Apps with this API
FREE REST API - Practice Developing Javascript AJAX Apps with this API
LearnCode.academy
41 Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
LearnCode.academy
42 Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
LearnCode.academy
43 Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
LearnCode.academy
44 Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
LearnCode.academy
45 Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
LearnCode.academy
46 Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
LearnCode.academy
47 Web Development Advice - Interview with Dev Tips
Web Development Advice - Interview with Dev Tips
LearnCode.academy
48 How the Internet Works for Developers - Pt 2 - Servers & Scaling
How the Internet Works for Developers - Pt 2 - Servers & Scaling
LearnCode.academy
49 How the Internet Works for Developers - Pt 1 - Overview & Frontend
How the Internet Works for Developers - Pt 1 - Overview & Frontend
LearnCode.academy
50 HAPROXY vs NGINX - 10,000 requests while killing servers
HAPROXY vs NGINX - 10,000 requests while killing servers
LearnCode.academy
51 Node.js Cluster - Boost Node App Performance & Stability with Clustering
Node.js Cluster - Boost Node App Performance & Stability with Clustering
LearnCode.academy
52 Web Dev Training with Treehouse
Web Dev Training with Treehouse
LearnCode.academy
53 What is Node.js Exactly? - a beginners introduction to Nodejs
What is Node.js Exactly? - a beginners introduction to Nodejs
LearnCode.academy
54 How to deploy node.js applications #1 - spin up a server
How to deploy node.js applications #1 - spin up a server
LearnCode.academy
55 Deploying node.js applications #2 - provision server & setup flightplan
Deploying node.js applications #2 - provision server & setup flightplan
LearnCode.academy
56 Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
LearnCode.academy
57 Mobile Web Design - Coding Workflow For Mobile Websites
Mobile Web Design - Coding Workflow For Mobile Websites
LearnCode.academy
58 WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
LearnCode.academy
59 GRUNT TUTORIAL - Grunt makes your web development better!
GRUNT TUTORIAL - Grunt makes your web development better!
LearnCode.academy
60 STOP USING FTP!  - How to Deploy with Flightplan over SSH
STOP USING FTP! - How to Deploy with Flightplan over SSH
LearnCode.academy

Related Reads

📰
Why SnapDOM Beats html2canvas for DOM-to-Image Capture
Learn why SnapDOM outperforms html2canvas for DOM-to-image capture and how to use it in your frontend projects
Dev.to · Juan Martin
📰
I built 42 landing page templates as single HTML files (no npm, no build step)
Learn how to create simple landing page templates as single HTML files without relying on npm or build steps, and why this approach matters for efficient web development
Dev.to · Segcam spa
📰
Part 7B — Section 2 — React Event Handling Explained: Forms, Event Object & User Input.
Learn React event handling for forms and user input to improve your frontend skills
Medium · JavaScript
📰
Stop Using window.resize for Everything: A Practical Guide to the ResizeObserver API
Learn to replace window.resize with the ResizeObserver API for more efficient and effective resize event handling
Medium · JavaScript
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →