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

Creating dynamic elements

Throughout all the chapters so far we avoided creating elements dynamically. Not anymore. In this chapter all of our artwork will be dynamically added onto the stage. Let's get started.

Video Transcript

00:00So far, we did our best to stick into elements that already were on the stage, but not any longer this last chapter
00:07We're gonna be creating dynamic elements adding them to the stage. So let's get started
00:11This is gonna get our prep to loops, but in the process, we're gonna learn how to create elements and add them to the stage
00:17Let's get things rolling. We're basically starting off with a gray page with a little box
00:23Let's see what we have here in our source files. You're gonna find our in CSS. I stripped everything down
00:29We only have your little box here and I've even tested it out by putting that span with a class of box right in there
00:36So let's begin. I'm gonna delete that box because I want to start from a completely fresh empty page
00:41And now what I want to do is I want to recreate it put it back in that box
00:48And really our goal is loops in this in this chapter
00:51So this and this lecture is gonna be really a prep for it
00:55But in the process, we're gonna learn about how to create dynamically elements and add them to the stage
01:00And we're gonna be using jQuery for that
01:03What I'm gonna do is I'm first. I'm just gonna create a new span
01:07No, stop in a new span and I'm really what I'm gonna do is I'm gonna use my document and I'm just gonna call create elements
01:16Which is a built HTML function and I'll just tell it. What do I want to create? I want to create a new span
01:21Once I create it. I really want to use jQuery
01:24So I'm gonna wrap this new creation around the dollar function because it's gonna add all those
01:29functionalities that just make life easier and what I want to do is I want to go to that span and I want to add a
01:35Class to it and the class that I want to add is the class box because that's the name of the class that we have
01:40in our CSS
01:42Next what I want to do is I want to set its position
01:45So I'm gonna set the span and I'm gonna set its CSS gonna add it dynamically
01:50I'm gonna let left position I will set at let's say 10 pixels or let's do it 50 pixels 50 pixels and
01:58I'm gonna do the same also for span CSS top
02:01I want to position it at 50 pixels from the top as well
02:06All that's left for me to do is to actually add it to the stage and to do that
02:10I'm gonna call the body function
02:12Well the jQuery and I'll call the body
02:15Because I want to add it directly into the body and I'm gonna add to the function append and all this stuff is completely new
02:21But it should be logical at this stage because we've been working with so many things at this stage
02:26But if it's not then just rewatch it because I'm gonna go through this quickly relatively speaking
02:32All right. So literally what I did and I'm just gonna walk through it quickly again
02:35I am creating the element with the document dot create element. I'm creating a new span
02:41I'm wrapping it around the jQuery and then saving it inside of variable called span
02:47and now I'm just approaching the functions of jQuery such as add class and
02:51setting that span to have the class of box and I'm
02:55Tweaking some CSS properties of the left in the top and setting both of them to position 50
03:00Once I'm done with that
03:01I'm approaching my body and I'm saying hey
03:03I want you to add the span to the end of this body and by the way
03:06You could have added it to any element or any anything anything you could put here in jQuery
03:10You could then append now to wrap this up before we move on to the next lecture
03:15What I want to do is I want to call tween max because what I want to do is I want to animate this element
03:20And I want it to expand. So I'm gonna go ahead and I'm just gonna call that span and
03:25I'm gonna say I want an animation of one second and I want it to scale
03:30But to two two times its size scale from zero one is the default and I want to scale it up to
03:36Two hundred percent or two in our case and that said that's all I really wanted to do at this stage
03:42If I go back to my browser and I click on refresh, we should see the box coming in and it's gonna grow
03:49okay, in the next lesson we're gonna take what we just did right now and
03:55Duplicate it many many times using loops and we're gonna see what loops are and how to work with them

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!