I have an element as below. When I apply .empty() method on the element it is removing the text "Name". Is there any other way to remove all child elements blindly from an element except its text ? I know the below example don't have any child elements.
<label class="field__label" for="1">Name</label>
Try this.
public static void main(String[] args) {
Element doc = Jsoup.parse(
"<div id='id'>"
+ "a"
+ "<div>b</div>"
+ "c"
+ "<div>d</div>"
+ "e"
+ "</div>");
Element e = doc.select("div#id").first();
e.select("*").remove(); // remove all children
System.out.println(e);
}
output:
<div id="id">
ace
</div>