document object model
gives you scripting access to all elements on a web page
DOM defines:
what can Java do (simple)
create, modify, and remove elements in the page dynamically
list of what java can do
the nodes in a document make up the page’s
DOM tree
DOM tree describes
relationships among elements
nodes are related to each other through
child-parent relationships
t/f: a node can have multiple children and multiple parents
false
multiple children, only one parent
nodes with same parent node are
siblings
html node in dom tree is called
root node, which is parent node of all HTML elements in a page
finding HTML elements in Java
(all document.)
getElementById, getElementsByTagName, getElementsByClassName
return all p elements as an array inside myElement
var x = myElement.document.getElementsByTagName(“p”);
return all elements with class=”major”
var x = document.getElementsByClassName(“major”);
finding html elements by css selectors
- p elements with class intro
var x = document.querySelectorAll(“p.intro”);
DOM has collections-
groups of related objects on a page
DOM collections examples
document.links/document.anchors, document.images, and document.forms
DOM collection’s length property specifies
the number of items in the collections
document.images[1]
second image in page
document.forms.length
number of forms in page
what is used to access element’s attribute values
dot (.) operator
use dot operator to access 3 different attributes (get element first)
var x = document.getElementById(‘myImage’);
x.src = ‘newpic.jpg’;
x.className=’allpics’;
x.id=’alternateimg’;
how to get user input value in form input element called fullName
var x = document.forms[0];
var y = x.fullName.value;
node properties
parentNode, childNodes[nodenumber], firstChild, lastChild, nextSibling, previousSibling
t/f: a child/sibling node can be a whitespace
true