I have method at my front-end, where I get HTML in string from back end
Here is it
private async tryGetOooMessage(employeeId: string): Promise<string> {
try {
return await firstValueFrom(this.messages.getOutOfOfficeInternalMessage(employeeId));
} catch (e) {
return '';
}
}
Result is:
<div dir="ltr">
<span>
<br>
</span>
</div>
<div dir="ltr">
<span>
<br>
</span>
</div>
<div dir="ltr">
<span>Dear colleagues! I would like to have a vacation from 14/06/2023 to 15/06/2023</span>
</div>
<div>
<p></p>
</div>
I need to delete all empty divs
before not empty. I mean all divs at start that has this
<div dir="ltr"><span><br> </span></div>
How I can do this correctly?
2
Answers
Depends a bit on the larger context, but I think this should get your started:
this code will correctly remove div elements that have no content, including those that only consist of whitespace characters. Iterate over the div elements in reverse order (from last to first) to avoid issues with modifying the DOM while iterating.