Upgrade to Pro

JavaScript Code to Run CSS, JS, and HTML Snippet

<!DOCTYPE html>
<html>
<head>
  <title>Run Snippet</title>
</head>
<body>
  <textarea id="html" placeholder="HTML code"><h1>Hello World</h1></textarea>
  <textarea id="css" placeholder="CSS code">h1 { color: blue; }</textarea>
  <textarea id="js" placeholder="JS code">console.log("JS loaded");</textarea>
  <button onclick="runCode()">Run</button>
  <hr>
  <iframe id="preview" style="width:100%; height:300px;"></iframe>

  <script>
    function runCode() {
      const html = document.getElementById('html').value;
      const css = document.getElementById('css').value;
      const js = document.getElementById('js').value;

      const finalCode = `
        <html>
          <head>
            <style>${css}</style>
          </head>
          <body>
            ${html}
            <script>${js}<\/script>
          </body>
        </html>
      `;

      const iframe = document.getElementById('preview');
      iframe.srcdoc = finalCode;
    }
  </script>
</body>
</html>

 How It Works:

  • The user types or pastes HTML, CSS, and JS into three <textarea> fields.

  • When the "Run" button is clicked, all code is combined into a single HTML document.

  • The result is injected into an <iframe> using srcdoc, allowing full rendering and execution.

Let me know if you want this adapted for React, Vue, or a code editor integration like CodeMirror or Monaco.

Flowisetech For easy access