Implementing Javascript
With the basic knowledge of HTML and CSS, it is now time to add in the language that allows users to interact with your website.
Learning Goals
- Adding Javascript to your website
- Embedding the <script></script> tag
- Event handlers with buttons
- Saying "Hello, World" with a button click
- Altering elements in your page
- Changing text using innerHTML
- Change an image source with a button click
- Using input fields
- Basic calculator
- Making a quick Mad Libs game
- Altering the CSS in your page
- onmouseover/onmouseout
Javascript In Your Website
Javascript is a powerful piece of the web development puzzle. Without it, many websites wouldn’t function. This isn’t the only language that adds functionality, but it is the most common in basic websites due to its simplicity and easy integration.
Adding <script> tags
Just like learning CSS in the previous web dev course, javascript has many ways of being used in a site
- Create a new project folder and call it something like "javascriptwebsite"
- Create a subfolder called "stylesheet".
- Create a file called "stylesheet.css" in that subfolder.
- Create the "index.html" file in the main folder (or root folder), and add the following code.
The <script></script> tags allow us to use javascript in our website.
Here, we added an "alert()" function that gave us a popup message when we first load in our page.
Handling Events
alert()
Basic alerts are cool and all, but how does this add functionality at all? The first step is to see
how the onclick property works.
A lot of this should look familiar, the function in the <script></script>
tag will display an alert with the message of a variable.
That variable’s value is determined in the <button></button> tags "onclick" property. You
should see that it is equal to a function called "sayHello()" (which was defined in the
<script></script> tags above.)
innerHTML
We’ve used the button to make alerts, but there is so much more we can do. Let’s explore a few of the different possibilities from a button click.
Changing Text
document.getElementById(): Using this function is how javascript can access the properties of
the HTML elements through their id.
Notice the paragraph tag with it’s id? That is how the getElementById() function is able to find
and change that value
innerHTML: Is exactly as it sounds. The HTML inside of the element
src
Other things that you can do is change pictures based on what was pressed.
Changing an image
Similar to how we change "innerHTML", but in this case we change the "src" property.
Input
Beyond buttons, input fields are a very common form of HTML elements that use javascript functionality. Because this one is a little more complicated, we’ll do this in multiple steps.
Calculator
<input> allows the user to enter information.
The "type" property determines what values
can be put in. In this case, numbers.
Notice the id set for both inputs and the
<p></p> tag, we will need this later.
The function called when the button is pressed.
parseInt(): A function that converts values
into numbers. This allows us to add these
values as numbers (otherwise they would be
combined together, for example 1 + 2 = 12)
Running your code you should be able to add two numbers and see your answer.
Mad Libs
Add words into the input and see your sentence change!
Not much different between this and what you did before. However, you’ll notice a few differences.
"input" is now "text" because we’re using words. We’re also placing an id in a <b></b> tag so
that we can change it’s HTML later.
CSS and JS
Hover
Final thing we will cover is a form of event listener and style changing through javascript.
First thing to notice is that the <script></script> tag is after the body instead of before it. This is
because we need the script to run after the elements have been created.
onmouseover/onmouseout: This is used to track when an element is being hovered or not
Requirements
Now that we’ve gone over some of the basic aspects of a website using Javascript, you are going to make a website based on whatever you would like! The requirements of this project are as follows:
- Make a basic website with HTML/CSS
- Follow the same rules for HTML/CSS that you learned in the last project
- Good file structure (index.html, sub folders, etc)
- Have a title
- Content must be in body
- Add one button that performs a function when clicked
- Button can change CSS, alter HTML content, or anything else that you think of
- Have at least 1 input fields with a button that does something with those values
- Cannot be the same calculator unless you add something new to it (like a 3rd number)
- Cannot be the Mad Libs game, unless you add something new to it
- Examples that you can do
- A questionnaire field asking for name, age, and other things
- Entering words in the field and pressing "Submit" to add those words somewhere on your page
- Have at least one element that does something when hovered over and when not hovered
- Can change CSS elements
- Can change words
- Can have an image change source
The world of javascript is huge, there are many cool ways to implement functionality in your website through javascript. Go to W3 Schools if you’re curious to see more of what you can do.
Challenges
None yet!
