I’m making a todo list with express/node. I’m having a hard time making the text strikethrough on click after you add something to do list, and my group is pretty stumped.
This project was originally created in with just html and javascript, but for another project we have to do, we had to make it working using node and mongo. I’m having a hard time making it strikethrough on click (as well as having nothing populate when there is no text added in the input bar. Right now, if you click, it’ll show empty spots, instead of showing nothing).
This is my index.ejs:
<ul id="ulForItems">
<% for(var i=0; i<tasks.length; i++) {%>
<% let lineThrough = tasks[i].completed ? 'line-through' : 'none' %>
<li>
<span style='text-decoration: <%= lineThrough %> '><%= tasks[i].task %></span>
<span class="complete"></span>
</li>
<% } %>
</ul>
This is my main.ejs:
let complete = document.getElementsByClassName("complete");
let trash = document.getElementsByClassName("delete");
Array.from(complete).forEach(function(element) {
element.addEventListener('click', function(event){
const listItem = this.parentNode.childNodes[1].innerText
fetch('/editTask', {
method: 'put',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
'task': listItem,
'completed' :false
})
})
.then(response => {
if (response.ok) return response.json()
})
.then(data => {
console.log(data)
window.location.reload(true)
})
});
});
Array.from(trash).forEach(function(element) {
element.addEventListener('click', function(){
fetch('/deleteList', {
method: 'delete',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
})
}).then(function (response) {
window.location.reload()
})
});
});
This is my server.js:
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));
app.get("/", (req, res) => {
db.collection("tasks")
.find()
.toArray((err, result) => {
if (err) return console.log(err);
console.log(result);
res.render("index.ejs", { tasks: result });
});
});
app.post("/addTask", (req, res) => {
db.collection("tasks").insertOne(
{
task: req.body.task,
completed: false,
},
(err, result) => {
if (err) return console.log(err);
console.log("saved to database");
res.redirect("/");
}
);
});
app.put("/editTask", (req, res) => {
console.log(req.body);
db.collection("tasks").findOneAndUpdate(
{ task: req.body.task, completed: false },
{
$set: {
completed: true,
},
},
{
sort: { _id: -1 },
upsert: false,
},
(err, result) => {
if (err) return res.send(err);
res.send(result);
}
);
});
app.delete("/deletelist", (req, res) => {
db.collection("tasks").deleteMany({}, (err, result) => {
if (err) return res.send(500, err);
res.send("Message deleted!");
});
});
Please let me know if you need more information!