Form elements
The DOM, DOM elements
Unobtrusive JavaScript
<button>
<button onclick="myFunction();">Click me!</button>
Button's text appears inside tag; can also contain images
To make a responsive button or other UI control:
document.getElementById
let name = document.getElementById("id");
document.getElementById
returns the DOM object for an element with a given id
(note that you omit the #
when giving an id)
document.getElementById
: An Example
<img id="pokeball" src="images/pokeball.jpg" alt="a pokeball" />
<button onclick="changeImage();">Click me!</button>
function changeImage() {
let pokeballImg = document.getElementById("pokeball");
pokeballImg.src = "images/mystery.gif";
}
<input>
<!-- 'q' happens to be the name of Google's required paramter -->
<input type="text" name="q" value="Colbert Report" />
<input type="submit" value="Booyah!" />
Input element is used to create many UI controls (an inline element that must be self-closed
name
attribute specifies name of query parameter to pass to server
type
can be button
, checkbox
, file
, hidden
, password
, radio
, reset
, submit
, text
, ...
value
attribute specifies control's initial text
<input> Text Fields
<input type="text" size="10" maxlength="8" /> NetID <br />
<input type="password" size="16" /> Password
<input type="submit" value="Log In!" />
input
attributes: disabled
, maxLength
, readonly
, size
, value
size
attribute controls onscreen width of text field
maxlength
limits how many characters the user is able to type into the field
<textarea>
<textarea rows="4" cols="20">
Type your comments here.
</textarea>
Initial text is placed inside textarea
tag (optional)
Required rows
and cols
attributes specify height/width in characters
optional readonly
attribute means text cannot be modified
A tree-shaped structure built out of all of the HTML elements in a page, accessible via JavaScript
<html>
<head>
<title> ... </title>
</head>
<body>
<h1> ... </h1>
<div>
<p> ... </p>
</div>
</body>
</html>
A set of JavaScript objects that represent each element on the page
Each tag in a page corresponds to a JavaScript DOM object
JS code can talk to these objects to examine elements' state
We can change state
div
We can change styles
document.getElementyById(...)
document.querySelector(...)
document.querySelectorAll(...)
document.createElement(...)
let pTag = document.getElementById("october");
For starters, the HTML attributes. This HTML:
<img src="images/puppy.png" alt="A fantastic puppy photo"/>
Has a DOM object (let's call it puppyImg
) with these two properties:
puppyImg.src
-- set by the browser to images/puppy.png
puppyImg.alt
-- set by the browser to "A fantastic puppy photo"
<p>See our <a href="sale.html" id="saleslink">Sales</a> today!</p>
<img id="icon" src="images/borat.jpg" alt="Borat" />
<caption class="photo user-upload">Beauty.</caption>
let icon = document.getElementById("icon");
let theLink = document.getElementById("saleslink");
let caption = document.querySelector("caption");
Property | Description | Example |
---|---|---|
tagName
|
element's HTML tag |
icon.tagName is "IMG"
|
className
|
CSS classes of element |
caption.className is "photo user-upload"
|
src
|
URL target of an image |
icon.src is "images/borat.jpg"
|
href |
URL target of a link |
theLink.href is "sale.html"
|
innerHTML
property
All DOM elements have a property called innerHTML
that has the contents of the HTML tag as a string:
<ul id="dr-seuss">
<li>Thing 1</li>
<li>Thing 2</li>
</ul>
let elm = document.getElementById("dr-seuss");
// elm.innerHTML : "\n Thing 1 \n Thing 2 \n"
Set their properties, and the page changes in response
This is how give behavior to web pages: use JavaScript to manipulate the DOM by changing the properties of DOM elements
<a id="fb-link" href="http://www.facebook.com">Facebook</a>
Before the JavaScript runs, we'd see:
And after we run this Javascript:
let link = document.getElementById("fb-link");
link.innerHTML = "MySpace is back in a really big way."
We'd see:
Now we'll see how to write unobtrusive JavaScript code
Allows separation of web site into 3 major categories:
<button onclick="okayClick();">OK</button>
// called when OK button is clicked
function okayClick() {
alert("booyah");
}
This is bad style (HTML is cluttered with JS code)
Goal: remove all JavaScript code from the HTML body
let objectName.onevent = function;
<button id="ok">OK</button>
let okButton = document.getElementById("ok");
okButton.onclick = okayClick;
It is legal to attach event handlers to elements' DOM objects in your JavaScript code
Notice that you do not put parentheses after the function's name
This is better style than attaching them in the HTML
<html>
<head>
<script src="myfile.js" type="text/javascript"></script>
</head>
<body> ... </body>
</html>
let x = 3;
function f(n) { return n + 1; }
function g(n) { return n - 1; }
x = f(x);
Your file's JS code runs the moment the browser loads the script
tag
At this point in time, the browser has not yet read your page's body
<html>
<head>
<script src="myfile.js" type="text/javascript"></script>
</head>
<body>
<div><button id="ok">OK</button></div>
(... more html ...)
let btn = document.getElementById("ok");
btn.onclick = okayClick; // this is bad: btn is null at this point
Problem: global JS code runs the moment the script is loaded
Script in head
is processed before page's body
has
loaded
No elements are available yet or can be accessed yet via the DOM
We need a way to attach the handler after the page has loaded...
window.onload
eventfunction functionName() {
// put code to initialize the page here
}
// instruct window to run the function when the page has loaded:
window.onload = functionName;
There is a global event called window.onload
event that happens once everything in the page has been loaded
If you attach a function as a handler for window.onload
, it will run at
that time
<button id="ok">OK</button> <!-- (1) -->
// called when page loads; sets up event handlers
function pageLoad() {
let ok = document.getElementById("ok"); // (3)
ok.onclick = okayClick;
}
function okayClick() {
alert("booyah"); // (4)
}
window.onload = pageLoad; // (2)
event names are all lowercase, not capitalized like most variables
window.
onLoad = pageLoad;
window.onload = pageLoad;
you shouldn't write ()
when attaching the handler
(if you do, it calls the function immediately, rather than setting it up to be called later)
ok.onclick = okayClick
();
ok.onclick = okayClick;
The JSLint checker will catch this mistake
Related: can't directly call functions like alert
;
must enclose in your own function
ok.onclick =
alert("booyah");
ok.onclick = okayClick;
function okayClick() { alert("booyah"); }
function(parameters) {
... statements ...;
}
JavaScript allows you to declare anonymous functions
Quickly creates a function without giving it a name
Can be stored as a variable, attached as an event handler, etc.
window.onload = function() {
let ok = document.getElementById("ok");
ok.onclick = okayClick;
};
function okayClick() {
alert("booyah");
}
or, the more concise, but perhaps harder to read:
window.onload = function() {
document.getElementById("ok").onclick = function() {
alert("booyah");
};
};
function okayClick() {
this.style.color = "red"; // <-- bad style
this.className = "highlighted"; // <-- better style
}
.highlighted { color: red; }
Well-written JavaScript code should contain as little CSS as possible
Use JS to set CSS classes/IDs on elements
Define the styles of those classes/IDs in your CSS file
let count = 0;
function incr(n) {
count += n;
}
function reset() {
count = 0;
}
incr(4);
incr(2);
console.log(count);
globals can be bad; other code and other JS files can see and modify them
How many global symbols are introduced by the above code?
count
, incr
, and reset
function everything() {
let count = 0;
function incr(n) {
count += n;
}
function reset() {
count = 0;
}
incr(4);
incr(2);
console.log(count);
}
everything(); // call the function to run the code
The above example moves all the code into a function; variables and functions declared inside another function are local to it, not global
How many global symbols are introduced by the above code?
everything
(can we get it down to 0?)
(function() {
statements;
})();
Wraps all of your file's code in an anonymous function that is declared and immediately called
0 global symbols will be introduced!
The variables and functions defined by your code cannot be messed with externally
(function() {
let count = 0;
function incr(n) {
count += n;
}
function reset() {
count = 0;
}
incr(4);
incr(2);
console.log(count);
})();
How many global symbols are introduced by the above code?
"use strict";
...your code...
Writing "use strict";
at the very top of your JS file turns
on strict syntax checking:
You should always turn on strict mode for your code in this class!
(function() {
// set-up code that doesn't involve the DOM
// (e.g. setting up initial values, arrays, etc.)
window.onload = function() {
// phew! your code goes here
};
//function definitions go here
})();