So I am working with a DOM challenge to improve my skills. I have reached the deliverable but would like to additionally only have 1 instance of each logged number run with the count increasing in that single string.
With what I have so far below, for each time my heart button is clicked during whichever count is present it will record and append that count as an li to my ul.likes.
How would I post only one li for each individual count and update the button click count within said specific li?
JS
const counter = document.querySelector("#counter");
const likes = document.querySelector(".likes");
function startCounter() {
setInterval(() => {
counter.textContent++;
}, 1000);
}
const minus = document.querySelector("#minus");
minus.addEventListener("click", () => {
counter.textContent--;
});
const plus = document.querySelector("#plus");
plus.addEventListener("click", () => {
counter.textContent++;
});
let lookup = {};
function buttonClicked() {
let li = document.createElement("li");
let activeCount = counter.textContent;
let likeList = document.querySelector("li");
if (lookup[activeCount]) {
lookup[activeCount]++;
} else lookup[activeCount] = 1;
li.innerHTML = `${activeCount} has been liked ${lookup[activeCount]} times!`;
likes.appendChild(li);
console.log(likes);
}
const heart = document.querySelector("#heart");
heart.addEventListener("click", () => {
buttonClicked();
});
const pause = document.querySelector("#pause");
startCounter();
HTML
<html>
<head>
<meta charset="UTF-8">
<title>Dom Challenge</title>
<style>
#counter {
background-color: black;
color: white;
display: table;
padding: 10px;
}
</style>
</head>
<body>
<h1>
Welcome to the DOM Challenge
</h1>
<h1 id='counter'>
0
</h1>
<button id='minus'> ➖ </button>
<button id='plus'> ➕ </button>
<button id='heart'> ❤️ </button>
<button id='pause'> pause </button>
<ul class='likes'></ul>
<div>
<h3>Comments</h3>
<div id='list' class='comments'></div>
<h3>Leave a comment</h3>
<form id="comment-form" action="">
<input type='text' name="comment" id="comment-input" cols="30" rows="10">
</br>
<button id='submit'>submit</button>
</form>
</div>
<!--<script src="js/index.min.js"></script>-->
<script src="js/challenge.js"></script>
</body>
</html>