OneCompiler

HTML Editor

149
 <!DOCTYPE html>
<html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Editor</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            height: 100vh;
            background-color: #f4f4f4;
        }
        .container {
            display: flex;
            width: 100%;
            height: 100%;
        }
        .editor {
            width: 60%;
            display: flex;
            flex-direction: column;
            padding: 20px;
            box-sizing: border-box;
            background-color: #ffffff;
            border-right: 1px solid #ccc;
        }
        .output-container {
            width: 40%;
            display: flex;
            flex-direction: column;
            padding: 20px;
            box-sizing: border-box;
            background-color: #f9f9f9;
        }
        .tabs {
            display: flex;
            border-bottom: 2px solid #ccc;
            margin-bottom: 10px;
        }
        .tab {
            padding: 10px;
            cursor: pointer;
            background-color: #ddd;
            margin-right: 10px;
            border-radius: 4px 4px 0 0;
        }
        .tab.active {
            background-color: #f44336;
            color: white;
        }
        textarea {
            width: 800px;
            height: 470px;
            padding: 10px;
            font-family: monospace;
            font-size: 14px;
            border: 1px solid #ccc;
            resize: none;
            margin-top: 10px;
        }
        .output {
            width: 100%;
            flex-grow: 1;
            border: 1px solid #ccc;
            background-color: white;
            overflow: auto;
            box-sizing: border-box;
        }
        .button-container {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
            margin-bottom: 10px;
        }
        button {
            width: 60px;
            height: 40px;
            font-size: 16px;
            cursor: pointer;
            background-color: #f44336;
            color: white;
            border: none;
            border-radius: 4px;
        }
        .preview-button {
            width: 80px;
        }
        button:hover {
            background-color: #e53935;
        }
    </style>
<style></style><style></style><style></style><style></style><style></style><style></style></head>
<body>

<div class="container">
    <!-- Input Section -->
    <div class="editor">
        <h1>HTML Editor</h1>
        
        <!-- Tabs -->
        <div class="tabs">
            <div class="tab active" onclick="switchTab('html')">index.html</div>
            <div class="tab" onclick="switchTab('css')">style.css</div>
            <div class="tab" onclick="switchTab('js')">script.js</div>
        </div>
        
        <!-- Text Areas -->
        <div id="htmlTab" class="tab-content">
            <textarea id="htmlInput">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Template&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;</textarea>
        </div>
        <div id="cssTab" class="tab-content" style="display:none;">
            <textarea id="cssInput" placeholder="Write your CSS code here"></textarea>
        </div>
        <div id="jsTab" class="tab-content" style="display:none;">
            <textarea id="jsInput" placeholder="Write your JavaScript code here"></textarea>
        </div>
    </div>

    <!-- Output Section -->
    <div class="output-container">
        <div class="button-container">
            <button onclick="updateOutput()">Run</button>
            <button class="preview-button" onclick="previewCode()">Preview</button>
        </div>
        <div class="output" id="htmlOutput"></div>
    </div>
</div>

<script>
    function switchTab(tab) {
        // Hide all tab contents
        document.querySelectorAll('.tab-content').forEach(function(tabContent) {
            tabContent.style.display = 'none';
        });

        // Remove active class from all tabs
        document.querySelectorAll('.tab').forEach(function(tabItem) {
            tabItem.classList.remove('active');
        });

        // Show the selected tab content
        document.getElementById(tab + 'Tab').style.display = 'block';

        // Add active class to the selected tab
        document.querySelector(`.tab:nth-child(${getTabIndex(tab)})`).classList.add('active');
    }

    function getTabIndex(tab) {
        switch (tab) {
            case 'html': return 1;
            case 'css': return 2;
            case 'js': return 3;
            default: return 1;
        }
    }

    function updateOutput() {
        var htmlCode = document.getElementById("htmlInput").value;
        var cssCode = document.getElementById("cssInput").value;
        var jsCode = document.getElementById("jsInput").value;

        var outputDiv = document.getElementById("htmlOutput");

        outputDiv.innerHTML = htmlCode;

        var styleTag = document.createElement('style');
        styleTag.innerHTML = cssCode;
        document.head.appendChild(styleTag);

        var scriptTag = document.createElement('script');
        scriptTag.innerHTML = jsCode;
        document.body.appendChild(scriptTag);
    }

    function previewCode() {
        var htmlCode = document.getElementById("htmlInput").value;
        var cssCode = document.getElementById("cssInput").value;
        var jsCode = document.getElementById("jsInput").value;

        var newWindow = window.open();
        newWindow.document.open();
        newWindow.document.write(htmlCode);

        var styleTag = newWindow.document.createElement('style');
        styleTag.innerHTML = cssCode;
        newWindow.document.head.appendChild(styleTag);

        var scriptTag = newWindow.document.createElement('script');
        scriptTag.innerHTML = jsCode;
        newWindow.document.body.appendChild(scriptTag);

        newWindow.document.close();
    }
</script>



<script></script><script></script><script></script><script></script><script></s