Professional Documents
Culture Documents
2 - Introduction To JS
2 - Introduction To JS
2 - Introduction To JS
Introduction
JavaScript to
KAINOS WORK EXPERIENCE
PETER MCAREE
What is JavaScript?
So far, we have covered off HTML & CSS. You should now know that both of these together,
are how webpages are structured and styled.
From the previous tutorials, you have created a number of different webpages using HTML
& CSS. These pages that you have created all display static content. They don’t respond to a
user clicking a mouse or typing a key.
Throughout this quick tutorial, you’ll learn about some of the functions of JavaScript and
learn how to apply them to the webpages that you’ve already created. You’ll also get to
explore a JavaScript library that has many built in functions that make our lives a lot easier
when it comes to programming.
2
Kainos Work Experience
Gettin’ Interactive
We’re going to be using the same IDE that we have previously been using – Visual Studio
Code.
Just like it colour coded and provided us with type-ahead for HTML and CSS, it will also do
the same with JS.
Go ahead and open up VS Code in the same directory that we have been using before – the
folder that you created on the desktop. Open up our index.html page that we created right
at the start, the code should look a little similar to this:
Go ahead and create two new buttons within the body of your HTML, just below the
paragraph that we have on line 11. Save your file and open it up in a browser, it should look
something like the following:
3
Kainos Work Experience
Create a new file called ‘app.js’ in the same way that we’ve created files before. You’ll
notice that JavaScript files have the extension .js, VS Code will recognise this.
Firstly, we need to link the JS file that we’ve just created to the HTML. This is done in a
similar way to linking CSS files to the HTML, take a look:
As you can see on line 5, we have inserted <script></script> tags. We have set the src
attribute to the name of our JS file ‘app.js’. By doing this, we can now access any functions
that we have written in our JavaScript file in our HTML.
Now that we have linked the two files, we need to use the function that we have just
created. Earlier on, you created two different buttons. What we want to do now is, when we
click one button, we want to change the background colour to red and when we click the
other one, we want to change it to yellow.
Add the following to your HTML code where you have created your buttons:
Save your files, view it in a browser and click the two buttons – does it work? What is
happening?
5
Kainos Work Experience