| HTML | Meaning and Content |
| CSS | Presentation & Style |
| JavaScript & jQuery |
Scripting, manipulating the DOM |
Excellent resource: W3 Schools
JavaScript Can Change HTML Content
document.getElementById("demo").innerHTML = "Hello JavaScript";
JavaScript Can Change HTML Styles (CSS)
document.getElementById("demo").style.fontSize = "35px";
JavaScript Can Hide HTML Elements
document.getElementById("demo").style.display = "none";
JavaScript Can Show HTML Elements
document.getElementById("demo").style.display = "block";
Primitive Types
A JavaScript string is zero or more characters written inside quotes.
Strings are very robust and we can manipulate them in a variety of ways. For simplicity we will stick to the basics.
Let's play with strings demo
Text
Variables
Declaration-> let age = 61
reassign -> age = 24
Operations -> let nextAge = age + 1
Mathematical Operators
Boolean Logical Operators
Comparison operators
A function is a block of reusable code written to perform a specific task. A function consists of a set of statements but executes as a single unit. In JavaScript, we have some browser built-in functions like alert(), prompt(), and confirm()
function nameOfFunction() {
//some code here....
}A function consists of:
function sayHello() {
console.log("Hello world");
}Here’s how to call the function:
sayHello();
//output: Hello world
Another example with parameters
function sum(num1, num2){
return num1 + num2;
}To invoke this function, we call it like this:
sum(1, 2);
//output: 3
Use the name of the function ->
<- Pass in values for the parameters
in the parentheses
JavaScript Can Change HTML Content
document.getElementById("demo").innerHTML = "Hello JavaScript";
JavaScript Can Change HTML Styles (CSS)
document.getElementById("demo").style.fontSize = "35px";
JavaScript Can Hide HTML Elements
document.getElementById("demo").style.display = "none";
JavaScript Can Show HTML Elements
document.getElementById("demo").style.display = "block";
Each browser Window object has the following:
Window object has:
This is what we are interacting with ->
This and the following pages use information from W3 Schools. There is a link to the content on the last slide of this section :)
When a web page is loaded, the browser creates a Document Object Model of the page. The HTML DOM model is constructed as a tree of Objects:
The HTML DOM is a standard for how to get, change, add, or delete HTML elements.
document.getElementById("list").style.listStyleType = "square";
id="list"
Before
After
HTML
<html lang="en">
<head>
<title>getElementById example</title>
</head>
<body>
<p id="para">Some text here</p>
<button onclick="changeColor('blue');">blue</button>
<button onclick="changeColor('red');">red</button>
</body>
</html>JavaScript function changeColor(newColor) { const elem = document.getElementById("para"); elem.style.color = newColor; }
ResultIn this example getElementById() is a method and elem.style.color accesses a property
| Method | Description |
|---|---|
| document.getElementById(id) | Find an element by element id |
| document.getElementsByTagName(name) | Find elements by tag name |
| document.getElementsByClassName(name) | Find elements by class name |
| Property | Description |
|---|---|
| element.innerHTML = new html content | Change the inner HTML of an element |
| element.attribute = new value | Change the attribute value of an HTML element |
| element.style.property = new style | Change the style of an HTML element |
| Method | Description |
| element.setAttribute(attribute, value) | Change the attribute value of an HTML element |
We can also add and delete elements, however we will not cover that this week. You can find more information about adding and deleting elements in your JavaScript & jQuery book :)
Online resources include:
You don't have to get or download JavaScript. JavaScript is already running in your browser on your computer, on your tablet, and on your smart-phone. JavaScript is free to use for everyone.
We do however need to reference our code in our files. Much like CSS, you can put a script within the body, in the head of the page or in a separate JavaScript file using the .js extension.
In most cases:
In the body
In the header
In an external .js file