Sometimes we need to get only the text from an HTML document, like the Rich Text format available for the text type inside Dataverse/Model-driven app.
If you need to perform this action client-side, a simple JavaScript function can be used:
function parseHTML(html) {
let doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || "";
}
function test_richtext_OnChange(executionContext) {
var formContext = executionContext.getFormContext();
let richTextValue = formContext.getAttribute("test_richtext").getValue();
let cleanValue = parseHTML(richTextValue);
formContext.getAttribute("test_plaintext").setValue(cleanValue);
}
It uses the DOMParser interface, here a screenshot on how the result looks like inside a model-driven app: