HTMLElement
In this chapter you will learn:
HTMLElement Members
HTMLElement
object represents the HTML elements in the document.
Name | Description | Returns |
---|---|---|
add(class) | Adds the class to the element. | void |
appendChild(HTMLElement) | Appends the specified element as a child of the current element. | HTMLElement |
attributes | Returns the attributes applied to the element. | Attr[] |
checked | Gets or sets the presence of the checked attribute. | boolean |
classList | Gets or sets the list of classes. | DOMTokenList |
className | Gets or sets the list of classes. | string |
cloneNode(boolean) | Copies an element. | HTMLElement |
compareDocumentPosition(HTMLElement) | Determines the relative position of an element. | number |
contains(class) | Returns true if the element belongs to the specified class. | boolean |
createElement(tag) | Creates a new HTMLElement object with the specified tag type. | HTMLElement |
createTextNode(text) | Creates a new Text object with the specified content. | Text |
dataset | Returns the data-* attributes. | string[name] |
dir | Gets or sets the dir attribute. | string |
disabled | Gets or sets the disabled attribute. | boolean |
getAttribute(name) | Returns the value of the specified attribute. | string |
hasAttribute(name) | Returns true if the element has the specified attribute. | boolean |
hidden | Gets or sets the hidden attribute. | boolean |
id | Gets or sets the id. | string |
innerHTML | Gets or sets the element's content. | string |
insertAdjacentHTML(pos, text) | Inserts HTML relative to the element. | void |
insertBefore(newelem, childElem) | Inserts the first element before the second (child) element. | HTMLElement |
isEqualNode(HTMLElement) | Determines whether the specified element is equal to the current element. | boolean |
isSameNode(HTMLElement) | Determines whether the specified element is the same as the current element. | boolean |
lang | Gets or sets the lang attribute. | string |
length | Returns the number of classes to which the element belongs. | number |
outerHTML | Gets or sets an element's HTML and contents. | string |
remove(class) | Removes the class. | boid |
removeAttribute(name) | Removes the specified attribute from the element. | void |
removeChild(HTMLElement) | Removes the specified child of the current element. | HTMLElement |
replaceChild(HTMLElement, HTMLElement) | Replaces a child of the current element. | HTMLElement |
setAttribute(name, value) | Applies an attribute with the specified name and value. | void |
spellcheck | Gets or sets the spellcheck attribute. | boolean |
style | the style of the element | CSSStyleDeclaration |
tabIndex | Gets or sets the value of the tabindex attribute. | number |
tagName | Returns the tag name indicating the element type. | string |
title | Gets or sets the title attribute. | string |
toggle(class) | Adds the class if it is not present, and removes it if it is present. | boolean |
The following code shows how to use HTMLElement to access element tag name, id, direction, language, hidden disabled information.
<!DOCTYPE HTML> <!-- ja v a2s . c o m-->
<html>
<body>
<p id="textblock" dir="ltr" lang="en-US">
<span id="banana">banana</span>
<span id="apple">apples</span>
<span="orange">oranges</span>
This is a test.
</p>
<pre id="results"></pre>
<script>
var results = document.getElementById("results");
var elem = document.getElementById("textblock");
results.innerHTML += "tag: " + elem.tagName + "\n";
results.innerHTML += "id: " + elem.id + "\n";
results.innerHTML += "dir: " + elem.dir + "\n";
results.innerHTML += "lang: " + elem.lang + "\n";
results.innerHTML += "hidden: " + elem.hidden + "\n";
results.innerHTML += "disabled: " + elem.disabled + "\n";
</script>
</body>
</html>
Next chapter...
What you will learn in the next chapter: