It seems to me this is a common job you do with templating engines.
In jsreport, templating engines like handlebars are used to dynamically assemble the html.
These engines are just javascript with some special marks, but it doesn't limit you in any way and you should be able to reach what you describe.
If you are new here, I recommend check some basic examples
https://playground.jsreport.net/
Our docs to handlebars
https://jsreport.net/learn/handlebars
And when you need to go deeper, check the handlebars official docs and google out handlebars examples
https://handlebarsjs.com/