jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners

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

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 -~-~~-~~~-~~-~-
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LearnCode.academy · LearnCode.academy · 19 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
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
25 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

This tutorial teaches how to write smarter jQuery code by targeting HTML elements by classname and getting custom HTML5 attribute values, making code more compact and efficient. It demonstrates how to use jQuery to select elements and get attribute values, and how to apply the 'do not repeat yourself' principle to reduce code duplication.

Key Takeaways
  1. Target HTML elements by classname using jQuery
  2. Get custom HTML5 attribute values using jQuery
  3. Apply the 'do not repeat yourself' principle to reduce code duplication
  4. Use the 'this' keyword to refer to the selected element
  5. Use the 'attr' method to get attribute values
  6. Combine strings using the '+' operator
💡 Using jQuery to target HTML elements by classname and get custom HTML5 attribute values can make code more compact and efficient, and applying the 'do not repeat yourself' principle can reduce code duplication.

Related Reads

📰
HTML, CSS, and JavaScript Roadmap: Where Every Developer Actually Starts
Learn the fundamentals of HTML, CSS, and JavaScript before moving to frameworks like React to build a strong foundation for a career in web development
Medium · JavaScript
📰
Your React App Works. Until the API Doesn’t.
Learn to handle API failures in React apps to ensure a seamless user experience
Medium · JavaScript
📰
We Hit the Browser's Scroll Limit at 550K Rows. Here's How We Reached 1 Million.
Learn how to overcome browser scroll limits when rendering large datasets in React, a crucial skill for frontend engineers
Dev.to · JANG KIYOUNG
📰
I accidentally reinvented CSS progress() and then I published it anyway.
Learn how a developer accidentally recreated CSS progress() and what you can learn from their experience in CSS framework development
Dev.to · Petros Papatheodorou
Up next
Adapting to dynamic content using modern CSS
Kevin Powell
Watch →