special thanks to Jan Michelfeit & Martin Kruliš
<script>
tag
<script type="text/javascript">
window.alert("Hello world");
</script>
<script>
tag
<script type="text/javascript" src="...url...">
</script>
<!doctype html>
<html>
<body>
<h1>DOM Example</h1>
<img src="img.png" alt="image"/>
</body>
</html>
<ul id="dom-list">
<!--Items: -->
<li>An item</li>
</ul>
document.getElementById("dom-list").childNodes
document.getElementById("dom-list").children
Selected methods & properties
document.getElementById(id)
-
a reference to the element by its ID
document.getElementsByTagName(tagName)
-
live HTMLCollection containing all elements of a particular tag name
element.getElementsByTagName(tagName)
-
live HTMLCollection containing all descendant elements of a particular tag name
node.children
-
live HTMLCollection of the child elements of Node.
node.firstChild
-
readonly property the node's first child in the tree, or null (not necessarily an element!)
node.lastChild
-
readonly property the node's last child in the tree, or null
node.nextSibling
-
the node immediately following the specified one
node.parentNode
-
the parent of the specified node in the DOM tree.
Selected methods & properties
document.createElement(tagName)
-
creates the HTML element specified by tagName
node.appendChild(childNode)
-
adds a node to the end of the list of children of a specified parent node
node.removeChild(childNode)
-
removes a child node from the DOM
node.cloneNode(deep)
-
returns a duplicate of the node on which this method was called
node.textContent
-
text content of a node and its descendants
element.innerHTML
-
sets or gets the HTML syntax describing the element's descendants
var link = document.getElementById("link");
link.addEventListener('click', function(event) { event.preventDefault(); } )
window.load
- document finished loadingnode.click
- node clickednode.keydown
- key pressednode.keyup
- key releasedelement.input
-
the value of an <input>
or <textarea>
element is changedform.submit
- form is submitted
Note that these are the event names. Attribute names have on
- prefix (e.g., onsubmit
).
employees
as <li>
items in content
list<h1>
headings so that they are numbered (i.e. "1. First heading", …)element.style
propertyelement.classList
div.style.fontWeight = "bold";
.important{ font-weight: bold; }
.hidden { display: none; }
div.className = "important";
div.classList.add("important");
div.classList.remove("important");
div.classList.toggle("hidden");
Download this document and fix TODOs