HTML/CSS/JS - Extended - Part I
08 Sep 2015##Overview
Last week we covered some new HTML elements and started with JavaScript. For a quick recap, HTML is the static form of a webpage, the CSS is what styles our webpage, and the JavaScript is what manipulates our HTML elements, as well as its CSS.
This week we will revisit what we did in the past two weeks and list other ways of doing the same thing. Also, we will cover more CSS and JavaScript and learn about their best practices.
##Previous Code
This is all the code we covered together, for in-depth explanation of the below snippets, go through Session 2 and Session 3.
###index.html
###style.css
###script.js
##Alternative Methods
There are few languages that restrict that way they execute. JavaScript is considered to be flexible compared to other languages. For example our function applyColour
can be written in 2 other ways:
Storing the function in a variable
Notice: The variable must be declared before applying it to the event listener.
Embedding the function into the event listenener
Notice: The drawback from this method is that the function cannot be reused somewhere else. In other words, since we didn’t store our function in some variable and replaced the variable in the event listener with the function directly, it will only affect the target element.
Storing the method is useful when you are sure that it will be referenced in more than one place in your script.
##Organizing Files
It is a good habit to organize your files and name them according to their purpose. Also, creating directories to separate the files is important. Therefore, let’s keep our index.html
page in our root directory (where the files are now), and let’s create a folder/directory and call it public
, in that folder create another two and call them css
and js
, to store all our CSS and JavaScript respectively.
Your project file should look something like this now:
Now we need to edit the path to the CSS and Javascript in our index.html
That’s about it! Now let’s dive more into JavaScript.
##Operators
We already covered the assignment operator (=) to assign values to variables, now let’s talk about arithmetic operators ( + - * / ).
We can use these operators between variables. Let’s say we have a percentage that we need to add to our result every time:
Other operators:
=
+=
->x += y
- Same as
x = x + y
- Same as
-=
->x -= y
- Same as
x = x - y
- Same as
*=
->x *= y
- Same as
x = x * y
- Same as
/=
->x /= y
- Same as
x = x / y
- Same as
%=
-> x %= y (remainder)- Same as
x = x % y
- Same as
There is also x++
and x--
which is equivalent to x = x +1
and x = x - 1
respectively.;
Comparison and Logical Operators:
==
equal to===
equal value and equal type!=
not equal!==
not equal value or not equal type>
greater than<
less than>=
greater than or equal to<=
less than or equal to
##Scopes
In JavaScript, scope is the set of variables, objects, and functions you have access to.
To better explain the above statement, let’s write down some examples:
Variable defined outside the function (GLOBAL Variable):
Variable defined inside the function and referenced outside of it (LOCAL Variable):
Automatic Global is when the value is assigned to a variable without the var
object. For example: x = 9
, anywhere in the script, even inside the function (try it out).
The Lifetime of JavaScript Variables:
- The lifetime of a JavaScript variable starts when it is declared.
- Local variables are deleted when the function is completed.
- Global variables are deleted when you close the page.
##JavaScript by Example
Now let’s learn more about JavaScript by doing a small exercise. Remember our fancy script that deals with percentage? Let’s create our own calculator that takes in any value and percentage, and outputs the answer for us.
Changes to index.html
:
- HTML element that takes in a number.
- HTML element that takes in a rate value.
- HTML element that is used to display our final result.
All the styling will go through our style.css
file in our CSS folder. And all the magic will happen inside our JavaScript file script.js
.
Add this in index.html
inside the body
tag before your script tag.
In our JS script we will have to create 2 functions; one to deal with the HTML values, their format and displaying the output, and another one to do all the math and return the result for us. Explanation will be available via the comments.
executeCalculator: Main function to be called in our event listeners.
calculate: We pass the input into here, and return the result to the calling function.
Now we simply attach executeCalculator as our event listener. We want to find the answer in real time, meaning as soon as a change occurs to any of our HTML input elements. The change
event is the right choice for this job.
For trying a list of other event listeners, visit this link to W3 Schools.
###That’s it! Now you have your first mini calculator and which addresses almost everything explained on this page!
#To be Continued - Part II: Styling using CSS
##References: