Iterate through HTML elements add a class name if it contains inline background-color

Advertisements I have a user generated rich text content that needs to be modify with some additional static class names. I tried below JS code. First I get the content and using DOMParser. Then iterate all elements, If any elements contains inline background-color, I need to add some random class name to that specific element… Read More Iterate through HTML elements add a class name if it contains inline background-color

Type 'Node | null' is not assignable to type 'Node' when trying to iterate over HTML elements in TypeScript

Advertisements I have some code takes a string of raw Markdown stored in ‘markdownString’ and converts it to HTML with the marked.js library for display on the web browser. I am trying to grab all plain text values from the page and to store it in an array of strings by using a TreeWalker and… Read More Type 'Node | null' is not assignable to type 'Node' when trying to iterate over HTML elements in TypeScript

How to disable parsing code in script tags using JavaScript DOMParser?

Advertisements I have the following text file with JavaScript tags: <script> … </script> <script> … </script> <script> … </script> when I parse it like so: const parser = new DOMParser(); const xmlDoc = parser.parseFromString(`<root>${data}</root>`, "text/xml"); const tags = xmlDoc.getElementsByTagName("script"); I get an error because there are < and > symbols in JS program code, like… Read More How to disable parsing code in script tags using JavaScript DOMParser?