Intro to Javascript

Lecture Overview

Intro

How Javascript Affects HTML and CSS

document.getElementById("demo").innerHTML = "Hello JavaScript";

Can Change Attribute Values

document.getElementById("demo").src = "somepng.png";

Changing CSS Values

document.getElementById("demo").style.display = "block";

Where to use it

Placing in the head

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>

<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>

</body>
</html>

Placing in the body

<!DOCTYPE html>
<html>
<body> 

<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>

<script>
function myFunction() {
 document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>

</body>
</html>

External Javascript

function myFunction() {
 document.getElementById("demo").innerHTML = "Paragraph changed.";
}
Advantages of using External Javascript

Variables

Example

var x;

x = 6;

Syntax

var x, y, z;          // How to declare variables
x = 5; y = 6;      // How to assign values
z = x + y;         // How to compute values

Most important rules for writing fixed values

Operators