jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
Skills:
JavaScript Fundamentals80%
Key Takeaways
This video tutorial demonstrates how to write smarter and better jQuery code by targeting HTML elements by classname and getting custom HTML5 attribute values, resulting in more compact and efficient code.
Full Transcript
okay so J querer number three uh if you didn't catch one and two they're in the description we're just going to move on uh so where we left off is we copied and pasted this piece of code four times because we were listening to four different buttons and we wanted to interact with four different panels it's not bad there's nothing wrong with this but it's terrible in that this thing is going to be huge if we're a little bit smarter we can actually make one thing that listens to all the buttons and talks to all the CL the panels so let's do this I'm going to show you kind of a couple different ways that you can do this uh and before we get into that I need to show you um how we can Target by attribute let me show you real quick what I mean by that I'm going to add this button and if you guys know from HTML 5 if you go data Dash you can make your own attributes I can go data Dash panel equals panel one and so then what I can do is I can actually find with J Creer I'm going to go over here to my console and just do it real time I'm going to look for a button that has an attribute of data panel equals panel one there you go and it found my button it's that first button so you can see if I use these brackets I can enter any attribute I can actually do this if I want to do um um ID I can actually search for idid because ID is an attribute ID equals btn1 so there you go same thing it found the panel and I found the button that has an ID of btn1 so that's how we search by uh attribute so I'm going to do now is I'm going to actually add data panel and I'm going to add the panel ID I'll actually go panel ID how's that makes a little more sense and then I'm going to put the ID of the panel that it relates to panel equal panel one panel two panel three panel four all right so let's get rid of this copy paste we're going to be smarter A good rule of web development is Dr y do not repeat yourself well don't repeat yourself this before this was not very dry we need to do what's called dry it up we need to stop repeating ourselves and write this one piece of code that does all the things that the copy and pasting would have done so now instead of button one let's get rid of these IDs here and we're going to add classes instead panel button so now these are all going to be a panel button because you'll know you can only have one ID but we can have multiple classes so now instead of targeting by ID we're going to Target by panel button and now this one rule is going to listen to All panel buttons whenever you click so now if I click on panel button one or if I click on button any of the buttons it should be toggling panel one so progress not where we want to be yet but we're getting there so now what we want to do is we want to do something that's called let's get the ID panel ID panel ID equals and we don't know which button was clicked cuz it might have been button one it might have been button two might have been button three might have been button four so we actually want to do is this no PR no Quotes no double quotes just this and so what that means whenever you do jQuery this it's going to take whatever the action was on and we're going to start using that guy so I can go this dot ATR data panel ID so that's going to look for an attribute of data- panel ID that's going to give me what that is so now panel ID if I go alert panel ID what it'll do is it'll get the panel ID and then it will alert it to me here I'll show you panel one if I click here panel three so now we know what panel we want to do something with great we're almost there um so instead of alerting panel ID I want to do hash Plus panel ID because I didn't store the hash up there I guess I could have stored the hash up there if I wanted to but for the sake of the lesson I'll show you how to put two strings together so we're going to put the hash Plus panel ID which doesn't do addition it just kind of bumps them together so hash Plus panel ID toggle so now we're going to listen to any panel button and when you click on it it's going to grab the panel ID out of that button and it's going to then go find that panel and toggle it see if it works nice nice and we win we dried it up we added one more line to our code and we have a much much cleaner thing uh you another way you can dry stuff up is say whenever you click this you want to show it and you want to you know give it some new content so what I can do is um ver content equals my new awesome content so now I can not only toggle it but I can make sure that panel ID Plus panel body HTML is content no no Quotes no double quotes which means I'm looking for a variable if I did quotes like this it would change it to the actual text of Co n tent which is kind of what it already is I don't want that I want it to look for a variable called content that I made up here I made a variable called content and I said it's my awesome new content so now it should there you go they're automatically going to get my new awesome content great another thing uh you can do is you can do HTML empty quotes to just kind of clean it out so now when you click on it nothing at all so that's kind of a way that we would dry up jQuery and start to reuse things that's also how you find out more information about who has clicked that this thing this I could also go this ATR ID if I wanted to find out the ID of whoever was clicked on um and that's going to alert my ID whoops can't do that because there are no IDs anymore I got rid of the IDS didn't I yep well if the IDS were still printed on here you'd be able to do that so anyway that's how you dry up jQuery
Original Description
With jQuery is easy to make things happen, but easy doesn't always mean right! In this lesson, we're gonna make our 20 lines of code compact into 4 lines of code...and it will be better code
This will involve selecting HTML elements by classname and getting a custom HTML5 attribute value with jQuery.
Lesson #1: https://www.youtube.com/watch?v=hMxGhHNOkCU&list=UUVTlvUkGslCV_h-nSAId8Sw
Lesson #2: https://www.youtube.com/watch?v=G-POtu9J-m4&list=UUVTlvUkGslCV_h-nSAId8Sw
Lesson #4: https://www.youtube.com/watch?v=LYKRkHSLE2E
-~-~~-~~~-~~-~-
Also watch: "Responsive Design Tutorial - Tips for making web sites look great on any device"
https://www.youtube.com/watch?v=fgOO9YUFlGI
-~-~~-~~~-~~-~-
Playlist
Uploads from LearnCode.academy · LearnCode.academy · 19 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
▶
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
59
60
Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
LearnCode.academy
Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
LearnCode.academy
How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
LearnCode.academy
Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
LearnCode.academy
Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
LearnCode.academy
CSS Tutorial - Web Development Tutorial for Beginners (#5)
LearnCode.academy
Node.js tutorial for beginners - an introduction to Node.js with Express.js
LearnCode.academy
Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
LearnCode.academy
Javascript Tutorial - Programming Tutorial for Beginners Pt 1
LearnCode.academy
Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
LearnCode.academy
AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
LearnCode.academy
WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
LearnCode.academy
YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
LearnCode.academy
BOWER! - Streamline Web Workflow with Bower Package Manager
LearnCode.academy
Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
LearnCode.academy
GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
LearnCode.academy
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
LearnCode.academy
Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
LearnCode.academy
jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #1 - jQuery Tutorial for Beginners
LearnCode.academy
Node.js MongoDB Tutorial using Mongoose
LearnCode.academy
Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
LearnCode.academy
WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
LearnCode.academy
jQuery Tutorial #4 - DOM Traversal with jQuery
LearnCode.academy
jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
LearnCode.academy
jQuery Tutorial #6 - Building a jQuery Image Slider
LearnCode.academy
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
LearnCode.academy
jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
LearnCode.academy
jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
How to put your website online - how to FTP to a domain & upload files to a webhost
LearnCode.academy
Basic Terminal Usage - Cheat Sheet to make the command line EASY
LearnCode.academy
SSH Tutorial - Basic server administration with SSH
LearnCode.academy
Vagrant Tutorial - Running a VM For Your Local Development Environment
LearnCode.academy
Sublime Text Favorite Packages and Workflow
LearnCode.academy
What Makes Javascript Weird...and AWESOME - Pt 1
LearnCode.academy
Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
LearnCode.academy
Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
LearnCode.academy
FREE REST API - Practice Developing Javascript AJAX Apps with this API
LearnCode.academy
Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
LearnCode.academy
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
LearnCode.academy
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
LearnCode.academy
Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
LearnCode.academy
Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
LearnCode.academy
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
LearnCode.academy
Web Development Advice - Interview with Dev Tips
LearnCode.academy
How the Internet Works for Developers - Pt 2 - Servers & Scaling
LearnCode.academy
How the Internet Works for Developers - Pt 1 - Overview & Frontend
LearnCode.academy
HAPROXY vs NGINX - 10,000 requests while killing servers
LearnCode.academy
Node.js Cluster - Boost Node App Performance & Stability with Clustering
LearnCode.academy
Web Dev Training with Treehouse
LearnCode.academy
What is Node.js Exactly? - a beginners introduction to Nodejs
LearnCode.academy
How to deploy node.js applications #1 - spin up a server
LearnCode.academy
Deploying node.js applications #2 - provision server & setup flightplan
LearnCode.academy
Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
LearnCode.academy
Mobile Web Design - Coding Workflow For Mobile Websites
LearnCode.academy
WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
LearnCode.academy
GRUNT TUTORIAL - Grunt makes your web development better!
LearnCode.academy
STOP USING FTP! - How to Deploy with Flightplan over SSH
LearnCode.academy
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
HTML, CSS, and JavaScript Roadmap: Where Every Developer Actually Starts
Medium · JavaScript
Your React App Works. Until the API Doesn’t.
Medium · JavaScript
We Hit the Browser's Scroll Limit at 550K Rows. Here's How We Reached 1 Million.
Dev.to · JANG KIYOUNG
I accidentally reinvented CSS progress() and then I published it anyway.
Dev.to · Petros Papatheodorou
🎓
Tutor Explanation
DeepCamp AI