java.lang.Object | ||
↳ | org.jsoup.nodes.Node | |
↳ | org.jsoup.nodes.Element |
Known Direct Subclasses |
A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements). From an Element, you can extract data, traverse the node graph, and manipulate the HTML.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Create a new, standalone Element.
| |||||||||||
Create a new Element from a tag and a base URI.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Add a class name to this element's
class attribute. | |||||||||||
Insert the specified HTML into the DOM after this element (i.e.
| |||||||||||
Add inner HTML to this element.
| |||||||||||
Add a node to the last child of this element.
| |||||||||||
Create a new element by tag name, and add it as the last child.
| |||||||||||
Create and append a new TextNode to this element.
| |||||||||||
Set an attribute value on this element.
| |||||||||||
Insert the specified HTML into the DOM before this element (i.e.
| |||||||||||
Get a child element of this element, by its 0-based index number.
| |||||||||||
Get this element's child elements.
| |||||||||||
Gets the literal value of this element's "class" attribute, which may include multiple class names, space
separated.
| |||||||||||
Get all of the element's class names.
| |||||||||||
Set the element's
class attribute to the supplied class names. | |||||||||||
Create a stand-alone, deep copy of this node, and all of its children.
| |||||||||||
Get the combined data of this element.
| |||||||||||
Get this element's HTML5 custom data attributes.
| |||||||||||
Get the list index of this element in its element sibling list.
| |||||||||||
Remove all of the element's child nodes.
| |||||||||||
Gets the first element sibling of this element.
| |||||||||||
Find all elements under this element (including self, and children of children).
| |||||||||||
Find an element by ID, including or under this element.
| |||||||||||
Find elements that have a named attribute set.
| |||||||||||
Find elements that have an attribute name starting with the supplied prefix.
| |||||||||||
Find elements that have an attribute with the specific value.
| |||||||||||
Find elements that have attributes whose value contains the match string.
| |||||||||||
Find elements that have attributes that end with the value suffix.
| |||||||||||
Find elements that have attributes whose values match the supplied regular expression.
| |||||||||||
Find elements that have attributes whose values match the supplied regular expression.
| |||||||||||
Find elements that either do not have this attribute, or have it with a different value.
| |||||||||||
Find elements that have attributes that start with the value prefix.
| |||||||||||
Find elements that have this class, including or under this element.
| |||||||||||
Find elements whose sibling index is equal to the supplied index.
| |||||||||||
Find elements whose sibling index is greater than the supplied index.
| |||||||||||
Find elements whose sibling index is less than the supplied index.
| |||||||||||
Finds elements, including and recursively under this element, with the specified tag name.
| |||||||||||
Find elements that directly contain the specified string.
| |||||||||||
Find elements that contain the specified string.
| |||||||||||
Find elements whose own text matches the supplied regular expression.
| |||||||||||
Find elements whose text matches the supplied regular expression.
| |||||||||||
Find elements whose text matches the supplied regular expression.
| |||||||||||
Find elements whose text matches the supplied regular expression.
| |||||||||||
Tests if this element has a class.
| |||||||||||
Test if this element has any text content (that is not just whitespace).
| |||||||||||
Retrieves the element's inner HTML.
| |||||||||||
Set this element's inner HTML.
| |||||||||||
Get the
id attribute of this element. | |||||||||||
Test if this element is a block-level element.
| |||||||||||
Gets the last element sibling of this element
| |||||||||||
Gets the next sibling element of this element.
| |||||||||||
Get the node name of this node.
| |||||||||||
Gets the text owned by this element only; does not get the combined text of all children.
| |||||||||||
Gets this node's parent node.
| |||||||||||
Get this element's parent and ancestors, up to the document root.
| |||||||||||
Add inner HTML into this element.
| |||||||||||
Add a node to the start of this element's children.
| |||||||||||
Create a new element by tag name, and add it as the first child.
| |||||||||||
Create and prepend a new TextNode to this element.
| |||||||||||
Gets the previous element sibling of this element.
| |||||||||||
Remove a class name from this element's
class attribute. | |||||||||||
Find elements that match the
Selector query, with this element as the starting context. | |||||||||||
Get sibling elements.
| |||||||||||
Get the Tag for this element.
| |||||||||||
Change the tag of this element.
| |||||||||||
Get the name of the tag for this element.
| |||||||||||
Gets the combined text of this element and all its children.
| |||||||||||
Set the text of this element.
| |||||||||||
Toggle a class name on this element's
class attribute: if present, remove it; otherwise add it. | |||||||||||
Get the value of a form element (input, textarea, etc).
| |||||||||||
Set the value of a form element (input, textarea, etc).
| |||||||||||
Wrap the supplied HTML around this element.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.jsoup.nodes.Node
| |||||||||||
From class
java.lang.Object
|
Create a new, standalone Element. (Standalone in that is has no parent.)
tag | tag of this element |
---|---|
baseUri | the base URI |
attributes | initial attributes |
Create a new Element from a tag and a base URI.
tag | element tag |
---|---|
baseUri | the base URI of this element. It is acceptable for the base URI to be an empty string, but not null. |
Add a class name to this element's class
attribute.
className | class name to add |
---|
Insert the specified HTML into the DOM after this element (i.e. as a following sibling).
html | HTML to add after this element |
---|
Add inner HTML to this element. The supplied HTML will be parsed, and each node appended to the end of the children.
html | HTML to add inside this element, after the existing HTML |
---|
Add a node to the last child of this element.
child | node to add. Must not already have a parent. |
---|
Create a new element by tag name, and add it as the last child.
tagName | the name of the tag (e.g. div ). |
---|
parent.appendElement("h1").attr("id", "header").text("Welcome");
Create and append a new TextNode to this element.
text | the unencoded text to add |
---|
Set an attribute value on this element. If this element already has an attribute with the key, its value is updated; otherwise, a new attribute is added.
attributeKey | The attribute key. |
---|---|
attributeValue | The attribute value. |
Insert the specified HTML into the DOM before this element (i.e. as a preceeding sibling).
html | HTML to add before this element |
---|
Get a child element of this element, by its 0-based index number.
Note that an element can have both mixed Nodes and Elements as children. This method inspects a filtered list of children that are elements, and the index is based on that filtered list.index | the index number of the element to retrieve |
---|
null
if absent.Get this element's child elements.
This is effectively a filter onchildNodes()
to get Element nodes.Gets the literal value of this element's "class" attribute, which may include multiple class names, space
separated. (E.g. on <div class="header gray">
returns, "header gray
")
Get all of the element's class names. E.g. on element <div class="header gray"
>},
returns a set of two elements "header", "gray"
. Note that modifications to this set are not pushed to
the backing class
attribute; use the classNames(java.util.Set)
method to persist them.
Set the element's class
attribute to the supplied class names.
classNames | set of classes |
---|
Create a stand-alone, deep copy of this node, and all of its children. The cloned node will have no siblings or parent node. As a stand-alone object, any changes made to the clone or any of its children will not impact the original node.
The cloned node may be adopted into another Document or node structure using appendChild(Node)
.
Get the combined data of this element. Data is e.g. the inside of a script
tag.
Get this element's HTML5 custom data attributes. Each attribute in the element that has a key starting with "data-" is included the dataset.
E.g., the element <div data-package="jsoup" data-language="Java" class="group">...
has the dataset
package=jsoup, language=java
.
This map is a filtered view of the element's attribute map. Changes to one map (add, remove, update) are reflected in the other map.
You can find elements that have data attributes using the [^data-]
attribute key prefix selector.
key=value
custom data attributes.
Get the list index of this element in its element sibling list. I.e. if this is the first element sibling, returns 0.
Remove all of the element's child nodes. Any attributes are left as-is.
Gets the first element sibling of this element.
Find all elements under this element (including self, and children of children).
Find an element by ID, including or under this element.
Note that this finds the first matching ID, starting with this element. If you search down from a different
starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
use getElementById(String)
id | The ID to search for. |
---|
Find elements that have a named attribute set. Case insensitive.
key | name of the attribute, e.g. href |
---|
Find elements that have an attribute name starting with the supplied prefix. Use data-
to find elements
that have HTML5 datasets.
keyPrefix | name prefix of the attribute e.g. data- |
---|
Find elements that have an attribute with the specific value. Case insensitive.
key | name of the attribute |
---|---|
value | value of the attribute |
Find elements that have attributes whose value contains the match string. Case insensitive.
key | name of the attribute |
---|---|
match | substring of value to search for |
Find elements that have attributes that end with the value suffix. Case insensitive.
key | name of the attribute |
---|---|
valueSuffix | end of the attribute value |
Find elements that have attributes whose values match the supplied regular expression.
key | name of the attribute |
---|---|
regex | regular expression to match agaisnt attribute values. You can use embedded flags (such as (?i) and (?m) to control regex options. |
Find elements that have attributes whose values match the supplied regular expression.
key | name of the attribute |
---|---|
pattern | compiled regular expression to match against attribute values |
Find elements that either do not have this attribute, or have it with a different value. Case insensitive.
key | name of the attribute |
---|---|
value | value of the attribute |
Find elements that have attributes that start with the value prefix. Case insensitive.
key | name of the attribute |
---|---|
valuePrefix | start of attribute value |
Find elements that have this class, including or under this element. Case insensitive.
Elements can have multiple classes (e.g. <div class="header round first">
. This method
checks each class, so you can find the above with el.getElementsByClass("header");
.
className | the name of the class to search for. |
---|
Find elements whose sibling index is equal to the supplied index.
index | 0-based index |
---|
Find elements whose sibling index is greater than the supplied index.
index | 0-based index |
---|
Find elements whose sibling index is less than the supplied index.
index | 0-based index |
---|
Finds elements, including and recursively under this element, with the specified tag name.
tagName | The tag name to search for (case insensitively). |
---|
Find elements that directly contain the specified string. The search is case insensitive. The text must appear directly in the element, not in any of its descendants.
searchText | to look for in the element's own text |
---|
Find elements that contain the specified string. The search is case insensitive. The text may appear directly in the element, or in any of its descendants.
searchText | to look for in the element's text |
---|
Find elements whose own text matches the supplied regular expression.
pattern | regular expression to match text against |
---|
Find elements whose text matches the supplied regular expression.
regex | regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options. |
---|
Find elements whose text matches the supplied regular expression.
pattern | regular expression to match text against |
---|
Find elements whose text matches the supplied regular expression.
regex | regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options. |
---|
Tests if this element has a class. Case insensitive.
className | name of class to check for |
---|
Test if this element has any text content (that is not just whitespace).
Retrieves the element's inner HTML. E.g. on a <div>
with one empty <p>
, would return
<p></p>
. (Whereas outerHtml()
would return <div><p></p></div>
.)
Set this element's inner HTML. Clears the existing HTML first.
html | HTML to parse and set into this element |
---|
Get the id
attribute of this element.
Test if this element is a block-level element. (E.g. <div> == true
or an inline element
<p> == false
).
Gets the last element sibling of this element
Gets the next sibling element of this element. E.g., if a div
contains two p
s,
the nextElementSibling
of the first p
is the second p
.
nextSibling()
, but specifically finds only ElementsGet the node name of this node. Use for debugging purposes and not logic switching (for that, use instanceof).
Gets the text owned by this element only; does not get the combined text of all children.
For example, given HTML <p>Hello <b>there</b> now!</p>
, p.ownText()
returns "Hello now!"
,
whereas p.text()
returns "Hello there now!"
.
Note that the text within the b
element is not returned, as it is not a direct child of the p
element.
Gets this node's parent node.
Get this element's parent and ancestors, up to the document root.
Add inner HTML into this element. The supplied HTML will be parsed, and each node prepended to the start of the element's children.
html | HTML to add inside this element, before the existing HTML |
---|
Add a node to the start of this element's children.
child | node to add. Must not already have a parent. |
---|
Create a new element by tag name, and add it as the first child.
tagName | the name of the tag (e.g. div ). |
---|
parent.prependElement("h1").attr("id", "header").text("Welcome");
Create and prepend a new TextNode to this element.
text | the unencoded text to add |
---|
Gets the previous element sibling of this element.
Remove a class name from this element's class
attribute.
className | class name to remove |
---|
Find elements that match the Selector
query, with this element as the starting context. Matched elements
may include this element, or any of its children.
getElementBy*
methods, because
multiple filters can be combined, e.g.:
el.select("a[href]")
- finds links (a
tags with href
attributes)
el.select("a[href*=example.com]")
- finds links pointing to example.com (loosely)
Selector
.query | a Selector query |
---|
Change the tag of this element. For example, convert a <span>
to a <div>
with
el.tagName("div");
.
tagName | new tag name for this element |
---|
Gets the combined text of this element and all its children.
For example, given HTML <p>Hello <b>there</b> now!</p>
, p.text()
returns "Hello there now!"
Set the text of this element. Any existing contents (text or elements) will be cleared
text | unencoded text |
---|
Toggle a class name on this element's class
attribute: if present, remove it; otherwise add it.
className | class name to toggle |
---|
Get the value of a form element (input, textarea, etc).
Set the value of a form element (input, textarea, etc).
value | value to set |
---|
Wrap the supplied HTML around this element.
html | HTML to wrap around this element, e.g. <div class="head"></div> . Can be arbitrarily deep. |
---|