Adding Inline JavaScript to HTML
After creating an image map it's time for us to create hot areas and define there "hot" clickable area. Some things are easier said then done so click on the pay button to see it in action. Once we created the hot area in the last session we continue and learn how to add JavaScript actions that will trigger when the user clicks onto our hot areas.
Video Transcript
00:00Alright, so while you were gone, we completed our areas, we did the two first together and
00:04we've completed the rest, figured out what the coordinates were.
00:09If you go back into the HTML file, you'll see that they're all clickable areas now.
00:14The next step is I want to create a JavaScript code that will change the background color
00:18based on what we click on.
00:19To do that, I'm going to go now into my JavaScript code, so we're going to go into
00:23that empty script tag that we have right now, and we're going to create together a
00:26function that changes backgrounds.
00:29Now, don't worry about the fact that you don't really know what functions are, nor do
00:32you know what JavaScript is, probably really in depth, but the idea itself of a programming
00:37language is to be able to enable you to create interactive experiences, really, or actions.
00:43And in this case, a function is a type of action.
00:46Now with that said, even if you don't know how to create your own JavaScript, hopefully
00:49this exercise, this example, will make it easier for you to find functions out there
00:55online and use them even without understanding how to do them yourself.
00:58So this is a little bit more advanced than what we've done so far, but don't worry
01:02about knowing how to do this, do this, do this, do this, do this on your own, all right?
01:07So I'm going to create a new function, and I'm going to call it change bg for change
01:11background.
01:12It's going to accept a color, or CLR, I'm going to get a color, it's basically a function
01:17that will get inside a value, and once we get that value, I'm going to go into
01:21my document, I'm going to go into my body, once I'm in my body, I'm going to
01:26change a style, and once I'm in my style, I'm going to change my background.
01:32And then I'm going to set the value of the color that I got into this function.
01:36Now again, this is a little bit advanced, if you were learning JavaScript, I wouldn't
01:40be talking to you about this until hour two or hour three of our training.
01:45So it's a little bit advanced, we have a lot of stuff inside of here.
01:48But all you really need to know about out of this code is now we have a function,
01:52we have a capability that enables us whenever we send a color inside of it to update the
01:58style sheet that's inside of our document body style background and change its color.
02:03That's more or less what you need to know out of this, that we have a function now
02:05that we could send a color inside of it, and it would automatically update the background
02:09of our style sheet.
02:11So now all that's left for me to do is to test it out.
02:14To test it out, I'm going to change the first hyperlink and just go inside of this
02:18href and instead of the right, JavaScript.
02:24By writing the word JavaScript and then colon, I'm basically telling the browser this is not
02:30a link really, this is an internal JavaScript call.
02:34Now that I said it's an internal JavaScript call, I could call my function change bg,
02:40open close brackets, and then use a semicolon and send into it whatever color I wanted to
02:46use.
02:47So in this case, I'm just going to send the color value, I'm going to put here, and
02:50again, this is a little bit advanced, but this is just for the sake of you seeing a
02:53full featured application, so I'm sending a string into it, and that's why I'm putting
02:58a double, I'm putting a quotation around it.
03:01The reason why I'm not putting a double quotation around it is, is because if I put
03:05a double quotation around it, it's going to, in this application, it's going to make
03:08it go crazy, but the reason is because we're already inside a double quotation, so I can't
03:13use a double quotation, it's going to get more tricky.
03:15So instead of that, I'm using a single quotation, which is accepted as well.
03:20But now at least I'm going to send into it a color.
03:22So one of the colors that I have already said in advance is BE1818, which is basically the
03:29blue, I believe it's the color right here in the side here, the red color actually,
03:34not blue, why did I get so blue?
03:36So when I click on it this time around, hopefully, our background will change the
03:39color if we did everything correctly.
03:43Oh, nice.
03:44Alright, so let's go through it again, just to make sure that you understand it,
03:47because it is advanced, and we are kind of like teaching you something that normally
03:50I wouldn't teach you, but it's kind of a teaser to make you want to come into our
03:53JavaScript course, it's we're really taking a leap of faith that you understand it
03:58because there's a lot of stuff that you need to learn before this would be really
04:01conveniently comfortable for you to understand.
04:04With that said, I do hope you understand the basics of it and how it works so you
04:07could see a live working JavaScript application.
04:10So the first thing that we said is we created a function, and the function
04:14basically is an action.
04:15We created an operation that enables us to change stuff in our application.
04:20We call the change bg, that's the name of our function, enabling us whenever we call
04:24change bg to send it a color, and it would automatically change the color of our
04:29background in our body.
04:30And this is basically HTML way of saying, JavaScript way of saying going to the
04:34HTML document into its body, into its style sheet, into its background, and then
04:40change its color.
04:42And once we have this function, we change our code in our href, and I'm just going
04:47to break a line here so to make it more easy to view, as we said, hey, we're not
04:52loading a link anymore.
04:53Instead of that, we're doing a JavaScript operation.
04:56What JavaScript operation are we doing?
04:58We're changing our background.
05:00And then we open and close a round bracket just like we did with the alert
05:04because that's how we call actions in JavaScript.
05:06And we had to send a string into it, and because we already had a double
05:10quotations outside, we couldn't just type here a double quotation because it
05:14would break our string because really, this double quotation, what does it do?
05:20It enables the browser to know that all of this belongs to the href.
05:25Now, if I put here another double quotation, the browser would think, oh,
05:29the href is done here.
05:31It's done.
05:31It's not needed anymore.
05:33And because of that, we couldn't use the double quotation anymore.
05:36But lucky for us, we could use the single quotation as well.
05:39So we use a single quotation instead, and then put inside a number,
05:42which we saw these numbers previously as well.
05:45So I'm going to duplicate this logic here, and I'm just
05:50going to change the color each time.
05:52So first of all, the color for the second item is not going to be the B-E-B-E,
05:55but instead of that, it's going to be 6-5-8-6-8-9.
05:59And no, I didn't memorize this.
06:01I have a cheat sheet.
06:04No, A-D.
06:07Let's do the next one as well.
06:09The next one, the value of it is going to be F-5-E-C-C-F. Let's do the next one.
06:18And again, it's not that I'm memorizing it.
06:20I actually checked the colors in Photoshop earlier.
06:22So I'm not that smart.
06:27Yes, you are.
06:29Thank you, Ma.
06:31All right, and the next one is we're going to put inside our last one.
06:36And our last one, the value of it would be O-B-1-D-31.
06:44I'm going to save that.
06:45If I go back into my browser, click on Refresh, we'll
06:48see that now any place that I click on, it should
06:50automatically update the background into whatever
06:53background we clicked on.
06:55All right, so we just created our first interactive
06:59JavaScript application.
07:00Obviously, I strongly encourage you at this stage to join our
07:03JavaScript course, but that's up to you.
07:06And if you want to create cool interactive things.
07:08Yeah, don't you have a job.
07:10Exactly.
07:11And congratulations.
07:12We're getting really close to the end of our HTML
07:14training, but there's a little bit more left.
07:16So we'll see you in the next video where we will
07:18learn about something new.
