Class Elements
- All Implemented Interfaces:
Serializable, Cloneable, Iterable<Element>, Collection<Element>, List<Element>, RandomAccess, SequencedCollection<Element>
-
Field Summary
Fields inherited from class AbstractList
modCount -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdd the class name to every matched element'sclassattribute.Insert the supplied HTML after each matched element's outer HTML.Add the supplied HTML to the end of each matched element's inner HTML.asList()Convenience method to get the Elements as a plain ArrayList.Get an attribute value from the first matched element that has the attribute.Set an attribute on all matched elements.Insert the supplied HTML before each matched element's outer HTML.clone()Creates a deep copy of these elements.comments()GetCommentnodes that are direct child nodes of the selected elements.GetDataNodenodes that are direct child nodes of the selected elements.deselect(int index) Remove the Element at the specified index in this list, but not from the DOM.Get the attribute value for each of the matched elements.eachText()Get the text content of each of the matched elements.empty()Empty (remove all child nodes from) each matched element.eq(int index) Get the nth matched element as an Elements object.expectFirst(String cssQuery) Just likeselectFirst(String), but if there is no match, throws anIllegalArgumentException.filter(NodeFilter nodeFilter) Perform a depth-first filtering on each of the selected elements.@Nullable Elementfirst()Get the first matched element.forms()Get theFormElementforms from the selected elements, if any.booleanChecks if any of the matched elements have this attribute defined.booleanDetermine if any of the matched elements have this class name set in theirclassattribute.booleanhasText()Test if any matched Element has any text content, that is not just whitespace.html()Get the combined inner HTML of all matched elements.Set the inner HTML of each matched element.booleanTest if any of the matched elements match the supplied query.@Nullable Elementlast()Get the last matched element.next()Get the immediate next element sibling of each element in this list.Get the immediate next element sibling of each element in this list, filtered by the query.nextAll()Get each of the following element siblings of each element in this list.Get each of the following element siblings of each element in this list, that match the query.Remove elements from this list that match theSelectorquery.parents()Get all of the parents and ancestor elements of the matched elements.Add the supplied HTML to the start of each matched element's inner HTML.prev()Get the immediate previous element sibling of each element in this list.Get the immediate previous element sibling of each element in this list, filtered by the query.prevAll()Get each of the previous element siblings of each element in this list.Get each of the previous element siblings of each element in this list, that match the query.remove()Remove each matched element from the DOM.remove(int index) Remove the Element at the specified index in this ist, and from the DOM.removeAttr(String attributeKey) Remove an attribute from every matched element.removeClass(String className) Remove the class name from every matched element'sclassattribute, if present.Find matching elements within this element list.@Nullable ElementselectFirst(String cssQuery) Find the first Element that matches theSelectorCSS query within this element list.Replace the Element at the specified index in this list, and in the DOM.Update (rename) the tag name of each matched element.text()Get the combined text of all the matched elements.GetTextNodenodes that are direct child nodes of the selected elements.toggleClass(String className) Toggle the class name on every matched element'sclassattribute.traverse(NodeVisitor nodeVisitor) Perform a depth-first traversal on each of the selected elements.unwrap()Removes the matched elements from the DOM, and moves their children up into their parents.val()Get the form element's value of the first matched element.Set the form element's value in each of the matched elements.Wrap the supplied HTML around each matched elements.Methods inherited from class Nodes
clear, deselect, deselectAll, outerHtml, remove, removeAll, removeIf, replaceAll, retainAll, toStringMethods inherited from class ArrayList
add, add, addAll, addAll, addFirst, addLast, contains, ensureCapacity, equals, forEach, get, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, removeFirst, removeLast, removeRange, size, sort, spliterator, subList, toArray, toArray, trimToSizeMethods inherited from class AbstractCollection
containsAllMethods inherited from interface Collection
parallelStream, stream, toArrayMethods inherited from interface List
containsAll, reversed
-
Constructor Details
-
Elements
public Elements() -
Elements
-
Elements
-
Elements
-
Elements
-
-
Method Details
-
clone
-
asList
Convenience method to get the Elements as a plain ArrayList. This allows modification to the list of elements without modifying the source Document. I.e. whereas callingelements.remove(0)will remove the element from both the Elements and the DOM,elements.asList().remove(0)will remove the element from the list only.Each Element is still the same DOM connected Element.
-
attr
Get an attribute value from the first matched element that has the attribute.- Parameters:
attributeKey- The attribute key.- Returns:
- The attribute value from the first matched element that has the attribute. If no elements were matched (isEmpty() == true), or if the no elements have the attribute, returns empty string.
- See Also:
-
hasAttr
-
eachAttr
Get the attribute value for each of the matched elements. If an element does not have this attribute, no value is included in the result set for that element.- Parameters:
attributeKey- the attribute name to return values for. You can add theabs:prefix to the key to get absolute URLs from relative URLs, e.g.:doc.select("a").eachAttr("abs:href").- Returns:
- a list of each element's attribute value for the attribute
-
attr
-
removeAttr
Remove an attribute from every matched element.- Parameters:
attributeKey- The attribute to remove.- Returns:
- this (for chaining)
-
addClass
-
removeClass
Remove the class name from every matched element'sclassattribute, if present.- Parameters:
className- class name to remove- Returns:
- this
-
toggleClass
Toggle the class name on every matched element'sclassattribute.- Parameters:
className- class name to add if missing, or remove if present, from every element.- Returns:
- this
-
hasClass
-
val
-
val
-
text
Get the combined text of all the matched elements.Note that it is possible to get repeats if the matched elements contain both parent elements and their own children, as the Element.text() method returns the combined text of a parent and all its children.
- Returns:
- string of all text: unescaped and no HTML.
- See Also:
-
hasText
Test if any matched Element has any text content, that is not just whitespace.- Returns:
- true if any element has non-blank text content.
- See Also:
-
eachText
-
html
-
tagName
-
html
-
prepend
-
append
-
before
-
after
-
wrap
Wrap the supplied HTML around each matched elements. For example, with HTML<p><b>This</b> is <b>Jsoup</b></p>,doc.select("b").wrap("<i></i>");becomes<p><i><b>This</b></i> is <i><b>jsoup</b></i></p> -
unwrap
Removes the matched elements from the DOM, and moves their children up into their parents. This has the effect of dropping the elements but keeping their children.This is useful for e.g removing unwanted formatting elements but keeping their contents.
E.g. with HTML:<div><font>One</font> <font><a href="/">Two</a></font></div>doc.select("font").unwrap();HTML =
<div>One <a href="/">Two</a></div>- Returns:
- this (for chaining)
- See Also:
-
empty
Empty (remove all child nodes from) each matched element. This is similar to setting the inner HTML of each element to nothing.E.g. HTML:
<div><p>Hello <b>there</b></p> <p>now</p></div>
doc.select("p").empty();
HTML =<div><p></p> <p></p></div>- Returns:
- this, for chaining
- See Also:
-
remove
Remove each matched element from the DOM. This is similar to setting the outer HTML of each element to nothing.The elements will still be retained in this list, in case further processing of them is desired.
E.g. HTML:
<div><p>Hello</p> <p>there</p> <img /></div>
doc.select("p").remove();
HTML =<div> <img /></div>Note that this method should not be used to clean user-submitted HTML; rather, use
Cleanerto clean HTML. -
select
-
selectFirst
Find the first Element that matches theSelectorCSS query within this element list.This is effectively the same as calling
elements.select(query).first(), but is more efficient as query execution stops on the first hit.- Parameters:
cssQuery- aSelectorquery- Returns:
- the first matching element, or
nullif there is no match. - Since:
- 1.19.1
- See Also:
-
expectFirst
Just likeselectFirst(String), but if there is no match, throws anIllegalArgumentException.- Parameters:
cssQuery- aSelectorquery- Returns:
- the first matching element
- Throws:
IllegalArgumentException- if no match is found- Since:
- 1.19.1
-
not
Remove elements from this list that match theSelectorquery.E.g. HTML:
<div class=logo>One</div> <div>Two</div>
Elements divs = doc.select("div").not(".logo");
Result:divs: [<div>Two</div>]- Parameters:
query- the selector query whose results should be removed from these elements- Returns:
- a new elements list that contains only the filtered results
-
eq
Get the nth matched element as an Elements object.See also
ArrayList.get(int)to retrieve an Element.- Parameters:
index- the (zero-based) index of the element in the list to retain- Returns:
- Elements containing only the specified element, or, if that element did not exist, an empty list.
-
is
-
next
-
next
-
nextAll
-
nextAll
-
prev
-
prev
-
prevAll
-
prevAll
-
parents
-
first
-
last
-
traverse
Perform a depth-first traversal on each of the selected elements.- Parameters:
nodeVisitor- the visitor callbacks to perform on each node- Returns:
- this, for chaining
-
filter
Perform a depth-first filtering on each of the selected elements.- Parameters:
nodeFilter- the filter callbacks to perform on each node- Returns:
- this, for chaining
-
forms
Get theFormElementforms from the selected elements, if any.- Returns:
- a list of
FormElements pulled from the matched elements. The list will be empty if the elements contain no forms.
-
comments
-
textNodes
-
dataNodes
-
set
Replace the Element at the specified index in this list, and in the DOM. -
remove
Remove the Element at the specified index in this ist, and from the DOM. -
deselect
-