Except where otherwise noted, the contents of this presentation are © Copyright 2007 Marty Stepp and are licensed under the Creative Commons Attribution 2.5 License.
<h2 onclick="myFunction();">Click me!</h2>
document
object and getElementById
<h2 onclick="makeRed();">Sell</h2> <p id="announce">Get it while it's hot!</p>
function makeRed() {
var para = document.getElementById("announce");
para.style.color = "red";
}
Get it while it's hot!
document
object's getElementById
method returns an object representing the HTML element with the given id
attribute (null
if not found)className
, id
, style
, title
style
property
function enlarge(id) {
var element = document.getElementById(id);
element.style.fontSize = "42pt";
}
Click me and make me big!
style
property represents the combined styles that apply to this elementbackgroundColor
, borderLeftWidth
, fontFamily
<button>
<button onclick="alert('Hello!');">Click me!</button>
button
tagonclick
event handler specifies button's behaviorinnerHTML
property
<button id="b1" onclick="myFunction('I did it!');">Click me!</button>
<p id="target">This text will be replaced.</p>
function myFunction(text) {
var p = document.getElementById("target");
p.innerHTML = text;
}
This text will be replaced.
innerHTML
refers to the HTML text inside of an element:<p>this is the innerHTML of the p tag</p>
innerHTML
of another element<button>
example
<button id="b1" onclick="addText();">Click me!</button>
function addText() {
var button = document.getElementById("b1");
button.innerHTML += " narf";
}
<button onclick="this.innerHTML += ' narf';">Click me!</button>
<textarea>
(DOM)<textarea rows="4" cols="20"> Type your comments here. </textarea>
textarea
tag (optional)readonly
attribute means text cannot be modifieddisabled
, readOnly
, value
value
, NOT innerHTML
<select>
(DOM), <option>
(DOM)<select> <option>Jerry</option> <option>George</option> <option>Kramer</option> <option>Elaine</option> </select>
option
element represents each choiceselect
optional attributes: disabled
, multiple
, size
onchange
handler to select
to cause behavior on each selection
<select onchange="alert('You chose ' + this.value);">
<select>
for lists
<select multiple="multiple" size="3">
<option value="Jerry">Jerry</option>
<option value="George">George</option>
<option value="Kramer">Kramer</option>
<option value="Elaine">Elaine</option>
<option value="Newman">Newman</option>
<option value="Susan">Susan</option>
</select>
disabled
, length
, multiple
, name
, selectedIndex
, size
, value
(selected item text)add(option, index)
, remove(index)
<optgroup>
<select> <optgroup label="Major Characters"> <option value="Jerry">Jerry</option> <option value="George">George</option> <option value="Kramer">Kramer</option> <option value="Elaine">Elaine</option> </optgroup> <optgroup label="Minor Characters"> <option value="Newman">Newman</option> <option value="Susan">Susan</option> </optgroup> </select>
<input>
<input type="text" /><br /> <input type="password" size="12" />
type
attributeaccept
, alt
, disabled
, maxlength
, name
, readonly
, size
, src
, type
, value
type="text"
and type="password"
: disabled
, maxLength
, readOnly
, size
, value
(text in field)<input type="radio" name="creditcards" id="visa" /> <label for="visa">Visa</label> <input type="radio" name="creditcards" id="mastercard" /> <label for="mastercard">MasterCard</label> <input type="radio" name="creditcards" id="amex" /> <label for="amex">American Express</label>
name
attributelabel
element with for
attribute set to button's id
checked
, defaultChecked
, disabled
<input type="checkbox" name="toppings" value="lettuce" id="lettuce" /> <label for="lettuce">Lettuce</label> <input type="checkbox" name="toppings" value="tomato" id="tomato" /> <label for="tomato">Tomato</label> <input type="checkbox" name="toppings" value="pickles" id="pickles" /> <label for="pickles">Pickles</label>
name
attribute is requiredchecked="checked"
attribute in HTML to initially check the boxchecked
(Boolean), defaultChecked
, disabled
<fieldset>
, <legend>
<fieldset> <legend>Credit cards:</legend> <input type="radio" name="creditcards" id="visa" /> <label for="visa">Visa</label><br /> <input type="radio" name="creditcards" id="mastercard" /> <label for="mastercard">MasterCard</label><br /> <input type="radio" name="creditcards" id="amex" /> <label for="amex">American Express</label><br /> </fieldset>
legend
supplies an optional caption