July 12, 2024

Rich Text to Plain Text conversion inside Power Apps

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:



Hope it helps!

1 comment:

  1. do you known how can we maintain the Paragraph spacing between the contents when extracting rich text to Simple Text.

    ReplyDelete