HTML JS Converter
Convert HTML to JavaScript as an escaped template literal or as document.createElement code, with {{placeholders}} as variables. Converts back too.
Still loading. If the tool doesn’t appear, reload this page.
Part of HTML to Code Converter
About this tool
HTML JS Converter
Convert HTML to JavaScript template code and convert JavaScript markup back into clean HTML placeholders.
Paste HTML and get JavaScript two ways: a template literal with backticks, backslashes and ${ escaped and {{ placeholders }} turned into escaped variables, or DOM-API code that builds the same markup with document.createElement, setAttribute and append — no innerHTML. The reverse mode pulls the HTML back out of a JS template. Everything runs in your browser.
How to use HTML JS Converter
Choose HTML to JS or JS to HTML, then type into Source code, press Load sample, or Upload code from a .html, .htm, .js, .jsx or .txt file.
Switch the toggles you need — Escape helper, Mount code, Base-path assets, Template variables, Strip script tags — and the output rebuilds as you type.
Read the Generated JS pane with its warnings, press Copy, or Download the file as converted.js — converted.html when running the reverse direction.
Why use HTML JS Converter
Escaping is handled in both directions
Backticks, backslashes and ${ are escaped going into the literal and decoded coming back out, which is exactly the part that breaks when done by hand.
Output is XSS-safe by default
Placeholders are wrapped in a generated escapeHtml helper that encodes &, <, >, " and ', instead of dropping raw values straight into innerHTML.
Every transform is a toggle
Strip script tags, add the mount code, route root-relative asset paths through a basePath variable or convert JS comments to HTML notes — each is independently switchable.
Common use cases
- You have a designer's static HTML page and need it as a JS render function, with the dynamic bits already wired to variables instead of retyping the markup inside backticks
- Reading a legacy widget where the whole UI is buried in one giant template literal, and you want the markup back as plain HTML you can open in a browser and edit
- Turning a Mustache or Handlebars-style partial into vanilla JS, so {{user.name}} becomes an escaped ${escapeHtml(userName)} rather than an unguarded interpolation
Frequently asked questions
How do I convert HTML to document.createElement code?
Switch on DOM API output. Each element becomes a const created with document.createElement, class becomes className, other attributes use setAttribute, single text children use textContent, and everything is appended to a fragment that is mounted on #app. Whitespace between tags is dropped.
Link to answerHow do I convert an HTML file into a JavaScript string?
Paste it and switch to HTML to JS: the markup is wrapped in a template literal as const html = `…`, with backticks, backslashes and ${ escaped so the string stays valid. You also get the mount snippet that assigns it to document.querySelector("#app").innerHTML.
Link to answerWhat happens to {{ placeholders }} in my HTML?
Each {{ key }} becomes ${escapeHtml(key)} with the key camel-cased into a valid identifier — {{user.name}} turns into ${escapeHtml(userName)} — and a const declaration is generated for every unique placeholder so the output runs as-is. Going the other way, ${escapeHtml(userName)} converts back to {{ user.name }}.
Link to answerWhy were my script tags removed?
Link to answerHow does JS to HTML find the markup in my file?
It looks for the first template literal assigned to a variable named html, markup or template, then for a .innerHTML = `…` assignment, then for a return `…`. If none of those match, the whole input is treated as markup and a warning says so — so wrap the markup in one of those three forms for a clean extraction.
Link to answerEmbed this tool on your site
Add the HTML JS Converter widget to your blog or website — free, responsive, no signup. Just keep the “Widget by AltFTool” credit link visible.
<iframe src="https://www.altftool.com/embed/widget/html-js-converter"
title="HTML JS Converter — free AltFTool widget"
width="100%" height="640" style="border:0;border-radius:12px;overflow:hidden"
loading="lazy" referrerpolicy="no-referrer-when-downgrade" allow="clipboard-write"></iframe>
<p style="font-size:12px;margin:4px 0 0">Widget by <a href="https://www.altftool.com/tools/all/html-js-converter?utm_source=embed&utm_medium=widget" rel="nofollow">AltFTool — free online tools</a></p>