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

Knowing what the user is doing

So far our main focus was on factual programming, and in many ways, even though our code was dynamic, it wasn't influenced much by user interaction. In this lecture and onwards, we move to face the users and interacting with them. By the end of this lesson, you'll have ample experience with events and user interactions.

Video Transcript

00:01So far we've seen a lot of things and we've seen a lot of user interaction, but very lame user interaction to be frank.
00:10How about us knowing when the user clicks on something? Or how do we know when users do things?
00:15And that's really going to be the topic of this lecture as we continue through the process of completing the task that we had for this chapter.
00:23So let's jump right in and see what events are.
00:27Events will be a rehearsal, but we have to learn another something really small.
00:31And that really small something that we need to learn is we need to basically be able to trigger something whenever the analyze button is clicked on.
00:39And I want you to see how easy it is. So don't worry, it's not as complicated as all the other stuff that we're going to go through and have been going through.
00:46Alright, so I'm going to go ahead and what I want to do is I want to refer back to that button, which is btn analyze.
00:54And what I want to do with that, I'm wrapping it inside of a jQuery command and then I call the on and that's it.
00:59That's the command on the first property is the event itself.
01:03Remember when we were talking, by the way, earlier, a lot earlier when we were first first course, we had the ready.
01:08So this is very similar. It's basically an event that will basically trigger a function with that event happens.
01:14And I could literally tell it what I want if it's a roll over, roll out, or in our case, I want it to happen on a click.
01:19So on click event, I want to call the analyze function.
01:23And there we go. We just we just created that. All that's left for me to do is to create the function analyze.
01:31And our function, our function analyze, we'll just do a console dot log, just so we could see that it's actually working.
01:40And I'm going to go ahead back into my browser, click, click, click on refresh and I go up a little bit of orange juice.
01:48And then I'll click on analyze and we'll see that our NARF is coming out there in our console.
01:55All right. So now that we know that it works, it's time for us to basically gather the information in our your name and our dates name.
02:02So let's go ahead. Let's get that. Let's get that baby rolling.
02:06So we want to access the basically T name and grab out of it its value.
02:11So I'm just going to go ahead into console log and let's try to get that value by approaching the T name through jQuery
02:20and asking for the text by calling the text function.
02:23And oh, not the T, not the text function actually, because this is not just regular HTML.
02:29It's actually an input field. So we want to use the valve function.
02:33So I'm going to go ahead there, type valve instead.
02:36And if I save this, go back into my browser, click on refresh, click on the analyze again and just type your Ben analyze.
02:45You'll see that we'll get the value Ben. It will give us whatever is here. Beautiful.
02:53So that's exactly what we wanted it to do. Now, the next step we want to do, obviously, we also want to get our date.
02:58That should be easy because it's going to be very similar to the way that we got access to our T name.
03:04We'll just basically duplicate this in our console just for now.
03:08And we want to get also our date so we know who our date is.
03:11Next, what we want to do is we want to basically kind of control it a little bit better because by default,
03:19if we look at our function, really, we don't want to analyze until both names are inside.
03:24So ideally, we probably want to have this disabled and this disabled until this copy is set out.
03:30So let's get out. Let's get ahead and get that sorted out.
03:33Let's go right back in there into our editor. And what we're going to do is we're going to go ahead
03:38and right before our click in it, to be honest, it doesn't really matter where we do this.
03:42I'm going to approach my jQuery again and just select the item.
03:46For example, we want to disable not the name, we want to disable the date.
03:49So let's go ahead and disable the date.
03:52And to do that, I'm just going to call the prop function and send to the property of disabled.
03:59And then I'll set the value of true. And that's it.
04:02And I'm going to do the exact same thing also for my click button.
04:05I want to basically disable that BTN analyze.
04:09So I'm going to go ahead and save that and refresh my browser.
04:12And this time around, we should see here buttons that don't work in a text field that you can't click on.
04:17Beautiful. So the next step that we want to do is we want to check to see whenever copy changes in this area.
04:23And whenever copy goes in, then we want to enable this copy immediately.
04:27So let's go ahead and add that property in.
04:30To do that, I'm going to basically approach this item and use on it an event,
04:34just like we did earlier to create an event whenever there is a click.
04:38So I'm going to go ahead and I want to put an event on my T name.
04:42So I'm going to wrap my T name.
04:46And next, I'm going to call the on.
04:48And this time around, I'm going to call the change property.
04:50And the change property will update every time something changes in our text field.
04:54And for that, I'm going to call on name change or on name.
04:59Yeah, on name change works for me.
05:01So on change name, let's do that on change name.
05:05And I'll just call it the call function on change name.
05:09And it's going to have an event that's going to give us the actual element that has been clicked on.
05:15By the way, the strokes are for the analyze.
05:18And I'm going to go ahead and I'm just going to console log the item that comes back.
05:23So let's just save this just so you could see it in action.
05:26And I'm just going to go ahead into my browser and click on refresh.
05:30And once our animation is complete, I'm going to come right in here.
05:33And as soon as I type on something, then nothing will actually happen until I actually tab out of it.
05:41So once I tab out of my item, in this case, you'll see that the change event has been triggered.
05:46And you can see all the different information about that event.
05:50Now, really, ideally, we probably would want this to happen way earlier as soon as copy changed inside.
05:56But for now, let's stick to this change event.
05:59And the next item is whenever that change happens, what we want to do is we want to actually check inside of our function
06:05to see what is our length of our name.
06:08So I'm going to just log it out here for a second.
06:10We're going to go into that T name.
06:13And what I'm going to do is I'm just literally and by the way, I could probably approach my element directly here
06:18and probably we could even see it inside here.
06:21I could literally click on it and then I'll be able to see more information.
06:25And I could just scan through it to basically see if I could reach the actual the target.
06:30And you can see here is our target.
06:32And in our target, we could search for our actual property that was changing, which is actually the text we want.
06:38Content Edible we could see is inherent.
06:40That doesn't really interest us too much.
06:43And we got their ID and on and on and on and on and on.
06:49Let's find it.
06:50I believe it should be in our.
06:59Here we go.
07:00Value.
07:01I was going to say value.
07:02I really was.
07:03All right. So what we could do is literally just instead of going referring in any other way,
07:08just set our target and then ask for a value and literally just output it directly into our logs.
07:14If we go and refresh it one more time, we could see that once anything changes inside of here.
07:20And if I just go back into my console and something changes inside of here and I tab out,
07:24we'll see that there was a change and we could see that item.
07:27If I tab out again, nothing will be outputted because nothing changed.
07:31Only once I change something, we'll see that it's going to trigger,
07:34but it only triggers once we step out of it, which is probably right,
07:37because one only when we're done actually copying.
07:40The only problem with that is we might click on this.
07:42And when we click on it, it's not going to be enabled.
07:44So that's definitely a problem.
07:46And we'll try to figure it out later.
07:47Or maybe I'll let you to figure out.
07:49We'll figure it out as we go.
07:50All right.
07:52So now that we saw that really our next step is basically just get the length.
07:57Because we want to know how much copy is in there.
07:59And again, I'm just going to refresh this just for a second.
08:01And really quickly, I'm just going to go into that item,
08:04put something inside of there and we could see that target length,
08:08length, value, length is not a function.
08:12All righty.
08:13So let's try it without, let's see what is a function.
08:16So I'm just going to go ahead and just make it not a function.
08:20Just ask for the length.
08:22As a property, I might have made a mistake and thought that it was a function.
08:27So let me just delete something.
08:28Click on tab.
08:29Here we go.
08:30So now we're getting the value, which is six.
08:31Beautiful.
08:32And that's exactly what we wanted.
08:33So all that's left for me to do is ask here if, if length,
08:38because if it's any value, even one, then we're good.
08:41And if it's one, then what I want to do is I want to enable,
08:44what do I want to enable?
08:45I want to enable the property of BTN8.
08:50And I want to enable that.
08:52So I could set that to false.
08:55And if I save this, go back into my application,
08:58click on refresh this time around.
09:01Whenever I change something in here and I click on tab,
09:04then this should become enabled.
09:07Now, obviously, ideally, we would love for it to just jump right away,
09:10but it's not.
09:11And we're not going to cover that right now.
09:12So the next thing that I want to do with this is what happens if I start my application.
09:16I want to basically automatically test to see if some copy was in here
09:20already originally initially.
09:22And to do that, what I'm going to do is I'll just automatically,
09:25even before anything changed, I'm just going to call the on change name,
09:29literally just calling it, not sending it anything inside.
09:32If I do that, I'm going to automatically call that function and automatically
09:36basically enable that element.
09:38And by the way, it's asking me for the element because it's expecting me to send
09:42that element inside.
09:44So in programming, one way for us to avoid that,
09:47and we know that we're actually not talking with it directly,
09:50but we are.
09:51So in our case, what we're going to have to do is we're going to have to fake
09:54that element.
09:55So literally, what I'm going to do is I'm just going to create an object.
09:58I'm going to give it a target.
10:00Tar.
10:01Yeah.
10:02And inside of the target, I'm going to create another object.
10:05And inside of there, I'm going to put a value.
10:08And I'm going to set the value to be basically our actual value.
10:13In this case, it would be our T name, T name.
10:19And this is really we're just doing this for the sake of basically
10:23seeing it in action.
10:25And I'm literally just going to get my value from there.
10:29Or I could have approached it just the way we did with jQuery in the jQuery
10:33way by calling the val function.
10:35So I'll just use the val function for this sake and purpose.
10:38And if we save this and run it one more time, it should basically send in a
10:41mimic of what would be sent from our array, making sure that it goes
10:44through without any hitch.
10:46Obviously, this is a very custom thing for you to do.
10:49And last but not least, we kind of have to make sure also that if
10:53something changes for our on change function,
10:57well, not on change.
10:58If something changes for our date, we want to do the same thing.
11:01So I'm going to pause here.
11:02In the next video, I'm going to have that already ready because we
11:05already did it.
11:06So what I want you to do is do it yourself and add it into the comments.
11:09I'm going to add that in as well, the logic of that as well.
11:12And you'll find that in the file in the end file of our chapter 6.
11:19And this is really it for the sake of really the basics of chapter 6.
11:25But we still wanted to cover that actual data processing itself.
11:29So because we want to actually add the data processing itself, what I'm
11:33going to do is I'm going to add that logic in.
11:35And I'm just going to continue as if you did it as well.
11:38So this is on you.
11:40You have to do it because if you don't, then you're going to be
11:42missing some code or you're going to have to search through it in the
11:45code that I've created myself.
11:46I'll talk about it for like three seconds in the next lesson.
11:48But I urge you to stop right now and to actually create this logic also
11:53for TDATE to make sure that whenever TDATE copy changes, we're going to
11:57enable our button again and making sure that it will automatically
12:00enable it if the copy is already in there when we start our application.
12:04All right.
12:05So see you in the next lecture.

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!