02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Leveraging CSS selectors in JavaScript

Traditional JavaScript developers had to work directly with the HTML DOM (Document Object Model). Lucky for us modern developers, we don't need to deal with this complicated structure (Although in the background it's exactly how it works). With modern libraries we can avoid interacting directly with this relatively complicated concept. Instead we're going to leverage the simplicity of CSS selectors to select documents elements. Both jQuery and GSAP come with an CSS Selector engine under the hood.

Hopefully by now you've worked with a bit of CSS. The way CSS elements integrate with HTML is through simple selector rules. In this lecture we're going to give you an overview of selectors and how to integrate them into our tweens.

Video Transcript

00:00We're taking a small break from functions, but not a very long one and we're definitely going to be using functions while we're doing it
00:07Our main topic is going to be selectors. Now before we even could talk about selectors
00:11I just wanted to say that
00:13Traditional developers in JavaScript a few years ago had to learn something that was called the document object model and really it's
00:21Terrifyingly complicated for most people mainly because it's just a lot of typing. It's not incredibly intuitive
00:29And it's not easy to travel through a document because really HTML
00:34Isn't a document and automatically we kind of could the browser converted it in a way, so we'll have access to it
00:41Now if you've done some JavaScript
00:44Well, you have because you've taken this course and you've if you've had done a little bit of CSS
00:49Then you'll know that CSS basically the way that it talks to HTML is through selectors now
00:56We're not going to learn every single selector out there
00:58We're going to see the basic most critical ones that will take you really far in programming in general. So let's get started
01:05It's an action. We're going to go into the code itself
01:08We've seen how to select one item. For example, we selected here our h2
01:12Which was our headline now again, if there were multiple headlines here
01:16It would automatically make every single headline to every single h2
01:21Whatever properties we set here in this case that the opacity to zero
01:26So let's see. How do you actually for example select a class?
01:30So in CSS the way you select the class is incredibly simple
01:34So we've seen how to select an element it could have been h2 it could have by the way
01:37It could have been oto p if we wanted to so every paragraph would be selected if we would save that go into our browser
01:43Click on refresh. We would see now our paragraph paragraph paragraph would become invisible
01:49Alright, so we're gonna leave that at h2 and let's look at the second thing that we could do second type of way
01:55access we could have in CSS and for that case in our animation library as the first property of the set property and by the
02:02Way oto the to and from which we're gonna look at again today and we've seen already earlier
02:07So if we wanted to select for example anything that has the class of book with slide
02:13All I have to do is just copy that class and instead of putting it directly the way it is
02:18I started up with a dot in the beginning now a dot in the beginning basically tells the browser or tells the
02:26CSS that this is a class. All right, so this is a class that we want to approach and if we set this class
02:32To opacity zero now both our headline and our paragraph is gonna be with opacity zero
02:38And by the way didn't this didn't have to be of a paragraph. It could have been of anything really
02:43So in this case if I do your refresh, we'll see that both my headline and my copy
02:49My paragraph are gonna be invisible the headline because we set our h2 to be invisible
02:55And the paragraph because we said anything with the class of book with style to be with opacity of zero
03:00Alright, let's continue to our last type of selector and this last type of selector is an ID type of selector
03:07So for example, if our image and I'm gonna go ahead and give it an ID if our image had an ID
03:13That was equaled something. So for example my image and
03:17This is a unique name. In other words only
03:20One item should have the ID of my image
03:24now in this case if I wanted to select that item all I had to do is start with a hashtag and then write my
03:30my image now I
03:46Want to talk about something that we didn't really talk too much throughout all this three chapters, but in
03:52JavaScript it's very important to stick with naming conventions
03:56If you set a camel case always use camel case if you set underscore or you use underscore
04:02the reason is and I'm just this is just for the sake of show showcasing is that we want to make sure that
04:09You're consistent and the more consistent you are whatever
04:12So the way you select to do things just stick with it
04:15And if you stick with it, you won't have as many bugs and things will be much easier
04:19The same is true with not only naming conventions
04:22If a certain class is tween max and it's a capital T and a capital M
04:27You cannot just go and call a lower T because it just wouldn't work same goes for your functions
04:32So if the function is set with all lowercase, you have to call it set with all lowercase
04:36You can't just call the s and uppercase because it wouldn't work. It's case sensitive
04:41Alright, so if I save this and I just run my application one more time and I click on refresh
04:46We're gonna see that now all three items are invisible
04:49Now I want to talk about another something that is called multiple selections
04:53Now there's a lot of other CSS rules and we're not gonna cover all of them because this course is not about
04:59CSS the more you're better at CSS the better you could create these really cool
05:05really complicated
05:06Selection types and literally anything any rule you know how to do in CSS
05:11You can apply oto outside inside of JavaScript using these methods set so and by the way
05:17It's also true for a jQuery, but this course isn't really about jQuery
05:22Alright, so what I want to do is I want to show you how do you do multiple selections in this case?
05:27I want to make all these three rules
05:29Automatically set an opacity of zero to do that
05:32All I have to do is put a comma in between rules and I could put basically all these three rules all in the same
05:39structure and
05:41The same true is is true with this. All right, so I could literally just put all the three items rule one
05:48Rule two rule three and all of them set to them the opacity of zero and if I refresh again
05:54You'll see that it's gonna work the exact same way as it worked previously. So that's it. That's all I wanted to talk about CSS selections
06:01No, there's obviously more selections and we'll probably see some of them as we progress throughout our courses
06:08but
06:09This is the critical stuff you need to know about in the following lecture. We're gonna continue talking about functions
06:15But this time around we're gonna create our own function, which is very
06:19Very exciting. So we'll see you in the next lesson where we continue working with functions
06:25we're gonna go back to the functions and
06:27Continue working with them to create our completed animation, but this time around help with the help of a function

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!