Currently, I have implemented two tab interfaces: one for entering HTML text and another for inputting CSS content. Following this, I have included a Preview section to visualize the combined output of both HTML and CSS elements. However, I am encountering an issue where only the HTML content is being displayed in the preview, while the CSS styling remains invisible.
https://i.sstatic.net/opwMT.png
Within my JavaScript function, I have defined the following logic:
function showPreview() {
var htmlContent = document.getElementById("editor").innerText;
var cssContent = "<style>" + document.getElementById("cssContent").value + "</style>";
var frame = document.getElementById("preview-window");
var dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(htmlContent + cssContent);
frame.src = dataURL;
}
showPreview()
I need assistance in identifying the error within my code and finding a resolution to ensure that both the HTML and CSS content are displayed correctly in the preview.
Sample Code Section
function showPreview() {
var htmlContent = document.getElementById("editor").innerText;
var cssContent = "<style>" + document.getElementById("cssContent").value + "</style>";
var frame = document.getElementById("preview-window");
var dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(htmlContent + cssContent);
frame.src = dataURL;
}
showPreview()
#editor,
#cssContent {
width: 456px;
height: 267px;
padding: 10px;
background-color: #d8d8d8;
color: rgb(0, 0, 0);
font-size: 14px;
font-family: monospace;
white-space: pre;
}
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30525f5f44434442514070051e031e02">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="40222f2f34333432213000756e716e73">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<div class="container">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">HTML</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">CSS</button>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div id="editor" contenteditable="true" oninput="showPreview();"><div>This is a Div</div>
</div>
</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<div id="cssContent" contenteditable="true" oninput="showPreview()"><style> div { background-color: blue; color: white; } </style>
</div>
</div>
<h3>PREVIEW</h3>
<div class="preview-area">
<iframe id="preview-window"></iframe>
</div>
</div>