Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.
a set of JavaScript objects that represent each element on the page
div
objectName.attributeName
document.getElementById
var name = document.getElementById("id");
<img id="doctor" src="tenth_doctor.jpg" alt="The Doctor" /> <br/> <button onclick="regenerate();"> Regenerate! </button>
function regenerate() { var img = document.getElementById("doctor"); img.src = "eleventh_doctor.jpg"; }
document.getElementById
returns the DOM object for an element with a given id
<div id="main" class="foo bar"> <p>Hello, <em>very</em> happy to see you!</p> <img id="icon" src="images/borat.jpg" alt="Borat" /> </div>
Property | Description | Example |
---|---|---|
tagName
|
element's HTML tag |
$("main").tagName is "DIV"
|
className
|
CSS classes of element |
$("main").className is "foo bar"
|
innerHTML
|
content inside element |
$("main").innerHTML is "\n <p>Hello, <em>ve...
|
src
|
URL target of an image |
$("icon").src is "images/borat.jpg"
|
<input id="sid" type="text" size="7" maxlength="7" /> <input id="frosh" type="checkbox" checked="checked" /> Freshman?
Property | Description | Example |
---|---|---|
value
|
the text in an input control |
$("sid").value could be "1234567"
|
checked
|
whether a box is checked |
$("frosh").checked is true
|
disabled
|
whether a control is disabled (boolean) |
$("frosh").disabled is false
|
readOnly
|
whether a text box is read-only |
$("sid").readOnly is false
|
value
vs. innerHTML
(8.2.1)
<p id="welcome">Go away, you're not welcome here.</p>
var paragraph = document.getElementById("welcome");
// change text inside an opening and closing tag
paragraph.innerHTML = "Welcome to our site!";
<input type="text" id="username" value="stepp" />
var textbox = document.getElementById("username");
// change text inside text box
textbox.value = "mdoocy";
Two ways to set the text of an element, depending on its type:
innerHTML
: set text between opening and closing tags (most regular elements)value
: set text inside a text box (form control)
textarea
s (which have opening and closing tags), only value
works as expected in all browsers<button onclick="swapText();">Click me!</button> <span id="output2">Hello</span> <input id="textbox2" type="text" value="Goodbye" />
function swapText() { var span = document.getElementById("output2"); var textBox = document.getElementById("textbox2"); var temp = span.innerHTML; span.innerHTML = textBox.value; textBox.value = temp; }
innerHTML
property
value
must be used instead
innerHTML
// bad style!
var paragraph = document.getElementById("welcome");
paragraph.innerHTML = "<p>text and <a href="page.html">link</a>";
innerHTML
can inject arbitrary HTML content into the pageinnerHTML
to inject HTML tags; inject plain text only
<button id="clickme">Color Me</button>
window.onload = function() { document.getElementById("clickme").onclick = changeColor; }; function changeColor() { var clickMe = document.getElementById("clickme"); clickMe.style.color = "red"; }
Property | Description |
---|---|
style
|
lets you set any CSS style property for an element |
camelCasedNames
backgroundColor
, borderLeftWidth
, fontFamily
.style
when setting styles
var clickMe = document.getElementById("clickme");clickMe.color = "red";clickMe.style.color = "red";
likeThis
, not like-this
clickMe.style.font-size = "14pt";clickMe.style.fontSize = "14pt";
clickMe.style.width = 200;clickMe.style.width = "200px"; clickMe.style.padding = "0.5em";
function okayClick() {this.style.color = "red";this.className = "highlighted"; }
.highlighted { color: red; }
JavaScript is a powerful language, but it has many flaws:
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js" type="text/javascript"></script>
$
function
(9.1.3)
$("id")
id
document.getElementById("id")
$("footer").innerHTML = $("username").value.toUpperCase();
method | description |
---|---|
setTimeout(function, delayMS);
|
arranges to call given function after given delay in ms |
setInterval(function, delayMS);
|
arranges to call function repeatedly every delayMS ms |
clearTimeout(timerID); clearInterval(timerID);
|
stops the given timer so it will not call its function |
setTimeout
and setInterval
return an ID representing the timer
clearTimeout
/Interval
later to stop the timer
setTimeout
example
<button onclick="delayMsg();">Click me!</button> <span id="output"></span>
function delayMsg() {
setTimeout(booyah, 5000);
$("output").innerHTML = "Wait for it...";
}
function booyah() { // called when the timer goes off
$("output").innerHTML = "BOOYAH!";
}
setInterval
example
<button onclick="delayMsg2();">Click me!</button> <span id="output2"></span>
var timer = null; // stores ID of interval timer function delayMsg2() { if (timer == null) { timer = setInterval(rudy, 1000); } else { clearInterval(timer); timer = null; } } function rudy() { // called each time the timer goes off $("output2").innerHTML += " Rudy!"; }
function delayedMultiply() {
// 6 and 7 are passed to multiply when timer goes off
setTimeout(multiply, 2000, 6, 7);
}
function multiply(a, b) {
alert(a * b);
}
setTimeout(multiply(6 * 7), 2000);
()
when passing the function
setTimeout(booyah(), 2000);setTimeout(booyah, 2000);setTimeout(multiply(num1 * num2), 2000);setTimeout(multiply, 2000, num1, num2);
()
?