So, I am trying to generate a web-page template with JavaScript according to user specified values. I tested it with just background-color and made it working.
I wanted to add a bunch of new elements. Also I need to add style to each elements. I am able to do it with the current code.
But the problem I am having is that the code is becoming "TOO HARD TO READ". And I don't think I'm writing clean code. So, the question I have for you fellow Dev's is that is there another way to achieve this - with just pure JavaScript (vanilla JS). That is:-
- To add specific elements separately.
- To style those elements also separately.
EDIT: I have updated the finished project. It's readable. But I would like to NOT use document.write as it is wasting time for me to read and understand. I would get stuck if I wanted to edit it later on... Please any other way you can suggest..
function renderForm(){
var bgc = document.getElementById("bgcolor").value;
var nc = document.getElementById("ncolor").value;
var tc = document.getElementById("tcolor").value;
var nm = document.getElementById("ntext").value;
window.alert("Confirm To NUKE!");
var generateTemplate = window.open("","_self");
generateTemplate.document.write('<!DOCTYPE html><html><head><title>Incoming Nuke!</title></head><body style="background-color:'+bgc+'">');
generateTemplate.document.write('<style>:root{ --main-nuke-color: '+nc+'; --main-text-color: '+tc+'; } .box{ position:absolute; display: block; height: 500px; width: 500px; animation-name: drop;animation-duration: 4s; animation-iteration-count: infinite;animation-direction:normal; animation-timing-function: linear; } .nhead{position: relative; display: block; height: 450px; width: 200px;border-radius: 50%; top: 20%; left: 15%; background-color:var(--main-nuke-color); transform: rotate(45deg); z-index: 9; } .nend{ position: absolute;display: block; width: 0; height: 0; border-top: 100px solid transparent;border-right: 100px solid var(--main-nuke-color); border-bottom: 100px solid transparent; top: 20%; left: 50%; transform: rotate(-45deg); } .ntailleft{position: absolute; display: block; width: 0; height: 0; border-top: 80px solid transparent; border-right: 80px solid var(--main-nuke-color);border-bottom: 80px solid transparent; top: 3%; left: 53%; transform:rotate(0deg); } .ntailright{ position: absolute; display: block; width: 0;height: 0; border-top: 80px solid transparent; border-right: 80px solid var(--main-nuke-color); border-bottom: 80px solid transparent; top: 23%; left:73%; transform: rotate(270deg); } .ntailmiddle{ position: absolute; display:block; width: 0; height: 0; border-top: 80px solid transparent; border-right:80px solid var(--main-nuke-color); border-bottom: 80px solid transparent;top: 10%; left: 65%; transform: rotate(135deg); } .text{ position: absolute;display: block; font-size: 90px; transform: rotate(-90deg); top: 35%; left:-62%; color: var(--main-text-color); word-wrap: break-word; white-space: nowrap; width: 430px; overflow: hidden; text-overflow: ellipsis; }div.text:hover { overflow: visible; }@keyframes drop{ 0%{ top: -50%; left: 100%; } 100%{ top: 100%; left: -50%; } }</style>');
generateTemplate.document.write('<div class="box"><div class="nhead"><div class="text">'+nm+'</div></div><div class="nend"></div><div class="ntailleft"></div><div class="ntailright"></div><div class="ntailmiddle"></div></div>');
generateTemplate.document.write('</body></html>');
}
function initMyEvents(){
document.getElementById("nuked").onclick = renderForm;
}
window.onload = initMyEvents;
@import url('https://fonts.googleapis.com/css2?family=Geo&display=swap');
body{
text-align: center;
background-color: #cccccc;
font-family: 'Geo', sans-serif;
font-size: 30px;
}
.customizerTable{
margin: auto;
}
.button{
border: none;
align-items: center;
background-color: rgb(204,204,204);
box-shadow: 2px 2px 4px 0 rgba(0, 0, 0, 0.25), -2px -2px 4px 0 rgba(255, 255, 255, 0.3);
border-radius: 50px;
display: flex;
justify-content: center;
margin: auto;
margin-left: 0;
margin-top: 5%;
padding: 5%;
width: 50%;
cursor: pointer;
outline: none;
text-decoration: none;
}
.button:active{
box-shadow: -2px -2px 4px 0 rgba(255, 255, 255, 0.3) inset, 2px 2px 4px 0 rgba(0, 0, 0, 0.25) inset;
}
.card{
border: none;
align-items: center;
background-color: rgb(204,204,204);
box-shadow: 2px 2px 4px 0 rgba(0, 0, 0, 0.25), -2px -2px 4px 0 rgba(255, 255, 255, 0.3);
border-radius: 50px;
display: flex;
justify-content: center;
margin: auto;
padding: 10%;
width: 100%;
}
.customizeField{
/*TEST 1*/
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/*TEST 1*/
}
.textbutton{
border: none;
align-items: center;
background-color: rgb(204,204,204);
box-shadow: -2px -2px 4px 0 rgba(255, 255, 255, 0.3) inset, 2px 2px 4px 0 rgba(0, 0, 0, 0.25) inset;
border-radius: 50px;
display: flex;
justify-content: center;
margin: auto;
margin-left: 0;
margin-top: 5%;
padding: 5%;
width: 50%;
cursor: pointer;
outline: none;
text-decoration: none;
}
<table class="customizeField">
<tr>
<td>
<div class="card">
<div>Customize</div>
<table class="customizerTable">
<tr>
<td>Background color:</td>
<td><input type="color" id="bgcolor" name="bgcolor" value="#80ccff" class="button"></td>
</tr>
<tr>
<td>Nuke color:</td>
<td><input type="color" id="ncolor" name="ncolor" value="#262626" class="button"></td>
</tr>
<tr>
<td>Text color:</td>
<td><input type="color" id="tcolor" name="tcolor" value="#e6e600" class="button"></td>
</tr>
<tr>
<td>Enter Nuke Code:</td>
<td><input type="text" id="ntext" name="ntext" maxlength="6" class="textbutton" value="#NUKED"></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
<a type="submit" value="Submit it!" class="button" id="nuked">NUKE!</a>
</td>
</tr>
</table>