DOM elements Flashcards
HTMLElementObject.accessKey = character
character: Specifies the shortcut key to activate/focus the element
document. getElementById(“myAnchor”).accessKey = “w”;
Sets or returns the accesskey attribute of an element
element. addEventListener(event, function, ?useCapture)
event: A String that specifies the name of the event. Note: Do not use the “on” prefix. For example, use “click” instead of “onclick”.
function: Specifies the function to run when the event occurs. When the event occurs, an event object is passed to the function as the first parameter. The type of the event object depends on the specified event. For example, the “click” event belongs to the MouseEvent object.
useCapture: A Boolean value that specifies whether the event should be executed in the capturing or in the bubbling phase.
Possible values:
true - The event handler is executed in the capturing phase
false- Default. The event handler is executed in the bubbling phase
Attaches an event handler to the specified element
node.appendChild(node)
ex:
var node = document.createElement(“LI”);
var textnode = document.createTextNode(“Water”);
node.appendChild(textnode);
document.getElementById(“myList”).appendChild(node);
Adds a new child node, to an element, as the last child node. Tip: If you want to create a new paragraph, with text, remember to create the text as a Text node which you append to the paragraph, then append the paragraph to the document.
node.attributes
Returns a NamedNodeMap of an element’s attributes
HTMLElementObject.blur()
Removes focus from an element
node.childElementCount
Returns the number of child elements an element has
element.childNodes
Returns a collection of an element’s child nodes (including text and comment nodes)
Tip: To return a collection of a node’s element nodes (excluding text and comment nodes), use the children property.
element.children
Returns a collection of an element’s child element (excluding text and comment nodes)
element.classList
.add - adds class name to element if not found .contains(class) - returns boolean .item(index) - returns class name at index .remove(class1, class2...) removes class(es), if nonexistent, no error .toggle(class, ?true|false) - adds or removes depending on if there; true/false forces add/remove
Returns the class name(s) of an element
HTMLElementObject.className = class
Sets or returns the value of the class attribute of an element. To apply multiple classes, separate them with spaces, like “test demo”
HTMLElementObject.click()
Simulates a mouse-click on an element
element.clientHeight
Returns the viewable height of an element, including padding
element.clientLeft
Returns the width of the left border of an element
element.clientTop
Returns the width of the top border of an element
element.clientWidth
Returns the viewable width of an element, including padding
node.cloneNode(?deep)
deep: Specifies whether all descendants of the node should be cloned.
true - Clone the node, its attributes, and its descendants
false - Default. Clone only the node and its attributes
Clones an element
node.compareDocumentPosition(node)
The possible return values would specify:
1: No relationship, the two nodes do not belong to the same document.
2: The first node (p1) is positioned after the second node (p2).
4: The first node (p1) is positioned before the second node (p2).
8: The first node (p1) is positioned inside the second node (p2).
16: The second node (p2) is positioned inside the first node (p1).
32: No relationship, or the two nodes are two attributes on the same element.
Note: The return value could also be a combination of values. I.e. the return value 20 means that p2 is inside p1 (16) AND p1 is positioned before p2 (4).
Compares the document position of two elements
node.contains(node)
Returns true if a node is a descendant of a node, otherwise false
HTMLElementObject.contentEditable = true|false
Sets or returns whether the content of an element is editable or not
HTMLElementObject.dir = “ltr|rtl|auto”
Sets or returns the value of the dir attribute of an element. The dir attribute specifies the text-direction (reading order) of the element’s content.
node.firstChild
Returns the first child node of an element
element.firstElementChild
Returns the first child element of an element (not text or comment node)
HTMLElementObject.focus()
Gives focus to an element
element.getAttribute(attributename)
var x = document.getElementsByTagName("H1")[0].getAttribute("class"); var x = document.getElementById("myAnchor").getAttribute("target");
Returns the specified attribute value of an element node
element.getAttributeNode(attributename)
ex: var elmnt = document.getElementsByTagName("H1")[0]; var attr = elmnt.getAttributeNode("class").value;
Returns the specified attribute node
Tip: Use the attribute.value property to return the value of the attribute node.
Tip: Use the getAttribute() method if you just want to return the attribute value.
element.getElementsByClassName(classname)
Returns a collection of all child elements with the specified class name
element.getElementsByTagName(tagname)
ex: var list = document.getElementsByTagName("UL")[0]; list.getElementsByTagName("LI")[0].innerHTML = "Milk";
Returns a collection of all child elements with the specified tag name
element.hasAttribute(attributename)
Returns true if an element has the specified attribute, otherwise false
node.hasAttributes()
Returns true if an element has any attributes, otherwise false