<!-- Created by Ishak -->
how can i stop the outputted color from appending to the child element? i want the chosen colour to be displayed on the list item after save is cliked but the colour moves each time a new list item is added, ive tried duplicating the element & list item if it already exists but i think im just making it more complicated…
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div id="myDIV" class="header">
</div>
<ul id="myUL">
<li>Hit the gym</li>
<li class="checked">Pay bills</li>
<li>Meet George</li>
<li>Buy eggs</li>
<li>Read a book</li>
<li>Organize office</li>
</ul>
<div id="box" class="box">
<header class="boxheader"></header>
<button class="cancel">Cancel</button>
<button id="save" onclick="functionA();
functionB();
" class="save" type="submit">Save</button>
</header>
<textarea name="td" id="myInput" class="inputarea" type="text" placeholder="Type something..."></textarea>
<div onclick="setallbtnflag();" id="allbtn" class="allbuttons">
<button id="redbtn" class="btnred" onclick="setredbtnflag();"></button>
</div>
</div>
<button id="add" class="add"
></button>
<div tagName="red" id="redbtnclone"></div>
<div id="grnbtnclone"
class="grnbtnclone"></div>
<div id="bluebtnclone" class="bluebtnclone"></div>
<div id="yellowbtnclone" class="yellowbtnclone"></div>
<div id="orangebtnclone" class="orangebtnclone"></div>
<script>
// initialize red btn flag
var redbtnflag = false;
var bluebtnflag = false;
var grnbtnflag = false;
var allbtnflag = false;
var yellowbtnflag = false;
var orangebtnflag = false;
var temp = 0;
var j = document.getElementById("redbtnclone");
// Create a "close" button and append it to each list item
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
var span = document.createElement("SPAN");
var txt = document.createTextNode("u00D7");
span.className = "close";
span.appendChild(txt);
myNodelist[i].appendChild(span);
}
// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if (ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
}
}, false);
// Create a new list item when clicking on the "Add" button
function functionA() {
var li = document.createElement("li");
li.appendChild(j);
var inputValue = document.getElementById("myInput").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
if(inputValue.length < 1) {
alert("You must write something!");
} else if (redbtnflag == false) {
alert("pls choose a colour!");
return false;
// code that will execute if all above conditions are false
} else {
document.getElementById("myUL").appendChild(li);
}
temp = document.getElementById("myInput").value
document.getElementById("myInput").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
}
</script>
<span id="vh" class="output"></span>
<script type="text/javascript">
//* when the red button is clicked, it will be displayed on the next page beside a list item after save is clicked
function setredbtnflag() {redbtnflag = true;}
function setgrnbtnflag() {grnbtnflag = true;}
function setallbtnflag() {allbtnflag = true;}
function setbluebtnflag() {bluebtnflag = true;}
function setyellowbtnflag() {yellowbtnflag = true;}
function setorangebtnflag() {orangebtnflag = true;}
//when add button is clicked, the box will hide or show
$('#add').click(function(){
$(".box").toggle();
});
// only toggle box when both the colour and input have been clicked
function functionB(){
if (temp.length < 1 && redbtnflag == true) {
redbtnflag = true
} else {
if (temp.length > 0 && redbtnflag == true) {
redbtnflag = false
// var li = document.createElement("li");
j.className = "redbtnclon";
//removes outline when buttonflag = false
document.getElementById("redbtn").style.outline = "";
document.getElementById("redbtn").style.border = "";
}
}
}
</script>
<style>
/* Created by Ishak */
body {
margin: 0;
min-width: 250px;
}
/* Include the padding and border in an element's total width and height */
* {
box-sizing: border-box;
}
/* Remove margins and padding from the list */
ul {
margin: 0;
padding: 0;
}
/* Style the list items */
ul li {
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
list-style-type: none;
background: white;
font-size: 18px;
transition: 0.2s;
margin-left:10px;
margin-right:10px;
box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
border-radius:3px;
margin-bottom:10px;
**
/* make the list items unselectable */
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Set all odd list items to a different color (zebra-stripes) */
ul li:nth-child(odd) {
background: #f9f9f9;
}
/* When clicked on, add a background color and strike out text */
ul li.checked {
background: #888;
color: #fff;
text-decoration: line-through;
}
/* Add a "checked" mark when clicked on */
ul li.checked::before {
content: '';
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
height: 15px;
width: 7px;
}
/* Style the close button */
.close {
position: absolute;
right: 0;
top: 0;
padding: 12px 16px 12px 16px;
}
.close:hover {
background-color: #f44336;
color: white;
}
/* Style the header */
.header {
background-color: green;
width:100%;
height:40px;
color: white;
text-align: center;
}
/* Clear floats after the header */
.header:after {
content: "";
display: table;
clear: both;
}
/* Style the input */
input {
margin: 0;
border: none;
border-radius: 0;
width: 75%;
padding: 10px;
float: left;
font-size: 16px;
}
.box{
width:95%;
height:360px;
border:none;
box-shadow: rgba(0, 0, 0, 0.09) 0px 2px 1px, rgba(0, 0, 0, 0.09) 0px 4px 2px, rgba(0, 0, 0, 0.09) 0px 8px 4px, rgba(0, 0, 0, 0.09) 0px 16px 8px, rgba(0, 0, 0, 0.09) 0px 32px 16px;
bottom:130px;
position:absolute;
left:9px;
background-color:#fff;
display:none;
}
.btnred{
width:35px;
height:35px;
background-color:#ff3300;
border-style:double;
border-radius:100%;
margin-right:10px;
border-color:#f06c6c;
}
.btngreen{
width:35px;
height:35px;
background-color:#66cc33;
border-radius:100%;
margin-right:10px;
border-style:double;
border-color:#3c8556;
}
.btnblue{
width:35px;
height:35px;
background-color:#409ade;
border-radius:100%;
margin-right:10px;
border-style:double;
border-color:#0554ff;
}
.btnyellow{
width:35px;
height:35px;
background-color:#e3c800;
border-radius:100%;
margin-right:10px;
border-style:double;
border-color:#e3cb19;
}
.btnorange{
width:35px;
height:35px;
background-color:#ff9933;
border-radius:100%;
margin-right:10px;
border-style:double;
border-color:#c77e08;
}
.allbuttons{
position:absolute ;
padding:20px;
width:90%;
height:80px;
right:1%;
bottom:20px;
}
.boxheader{
background-color:white;
width:100%;
height:50px;
box-shadow: rgba(60, 64, 67, 0.3) 0px 1px 2px 0px, rgba(60, 64, 67, 0.15) 0px 1px 3px 1px;
}
.save{
position:absolute;
background-color:transparent;
border:none;
color:#1278b8;
top:20px;
left:80%;
}
.cancel{
position:absolute;
background-color:transparent;
border:none;
color:#1278b8;
top:20px;
right:80%;
}
.inputarea{
width:100%;
height:50%;
border:none;
padding:10px;
margin-top:5px;
border:1px solid black;
}
.add{
background-color:green;
color: white;
padding: 16px;
font-size: 16px;
border: none;
border-radius:100%;
width:60px;
height:60px;
margin-left:270px;
top:430px;
position:fixed;
}
output color
.redbtnclon{
width:35px;
height:35px;
background-color:#ff3300;
border-radius:100%;
border-color:#f06c6c;
}
.grnbtnclon{
width:35px;
height:35px;
background-color:#66cc33;
border-radius:100%;
}
</style>
</body>
</html>