What is the best way to incorporate the ability to strikethrough items in my task list?

I am currently working on a project to create a dynamic To-Do-List using Express and EJS. My goal is to implement a feature in the To-Do-List where checking a checkbox will cause the corresponding content to be striked-through.

Initially, my approach involved using body-parser to retrieve the checkbox value, store it in a variable, and then pass it to my index.ejs file. However, when I attempted to use the variable in the EJS file, it did not yield the desired outcome. Upon further investigation, I found that the displayed value was "undefined".

import express from "express";
import bodyParser from "body-parser";

const app = express();
const port = 3000;
const Item = [];


app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));


app.get('/',(req,res)=>{
    res.render("index.ejs",{Item})
})

app.post('/submit',(req,res)=>{
    const {Item: newData} = req.body;
    const checked = req.body["check"];
    Item.push(newData)
    res.render("index.ejs",{Item,checked})
    console.log(checked)
})
app.listen(port,()=>{
    console.log("Server is running on port",port);
})


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <title>ToDoList</title>
  </head>
  <body>
    <div class="container">
      <form action="/submit" method="POST">
        <input
          type="text"
          placeholder="New Item"
          name="Item"
          class="input-area"
          required
        />
        <input type="submit" class="btn" value="Add" />
      </form>
    </div>
    <% Item.forEach(item => { %>
    <div class="container-2">
      <input type="checkbox" class="checkBox" name="check" required />
      <%= item %>
    </div>
    <% }) %>
  </body>
</html>

Answer №1

To apply a strikeout effect to your text, wrap it in a span element and use the following CSS:

.container-2 input:checked~.checkboxtext {
  text-decoration: line-through;
}
<div class="container">
  <form action="/submit" method="POST">
    <input type="text" placeholder="New Item" name="Item" class="input-area" required />
    <input type="submit" class="btn" value="Add" />
  </form>
</div>
<div class="container-2">
  <input type="checkbox" class="checkBox" name="check" required />
  <span class="checkboxtext">TODO 1</span>
</div>
<div class="container-2">
  <input type="checkbox" class="checkBox" name="check" required />
  <span class="checkboxtext">TODO 2</span>
</div>
<div class="container-2">
  <input type="checkbox" class="checkBox" name="check" required />
  <span class="checkboxtext">TODO 3</span>
</div>

Answer №2

Your code is a bit confusing because everything is named as a variable of "Item." It would be helpful to give more distinctive names to avoid confusion.

However, in your HTML, "Item" is an object (found on line 18 of the first block) that should contain the boolean value you are looking for under the key 'checked.'

Try using something like

<%= item.check ? 'checked' : ''%>
in your forEach loop on the input element in the HTML. This code essentially checks if the item.check value exists and is true, then adds 'checked' to the HTML input element, otherwise, it does not.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with a Jquery/Javascript Dilemma

As a beginner in query/javascript, I am encountering an issue with the code below when trying to calculate gross value and tax amount based on the net amount entered by the user. The input is expected to be a double amount, with gross and VAT amounts defin ...

The system is currently unable to find the specified element

I am facing an issue trying to locate a button that is defined under a specific class using XPATH. The error message "Unable to locate element" keeps popping up. Here are the details of the class: <div class="aui-button-holder inputBtn" id="aui_3_4_0_1 ...

Using CSS or Javascript, you can eliminate the (textnode) from Github Gist lists

My goal is to extract the username and / values from a series of gists on Github Gists. The challenge lies in the fact that there are no identifiable classes or IDs for the / value. https://i.stack.imgur.com/9d0kl.png Below is the HTML snippet with a lin ...

Presentation: Troubleshooting Ineffective CSS3 Transitions

I have created a basic slideshow that can accommodate multiple images. Clicking on an image should move to the next slide, and once the last image is clicked, it should loop back to the first slide. The functionality of transitioning between slides seems ...

PHP fails to retrieve posted data from AJAX when using serializeArray()

I have been struggling with a form that is utilizing jQuery validation and AJAX to submit data to a PHP script for backend processing. The AJAX function uses serializeArray() to collect the form values, but despite my best efforts, I cannot seem to success ...

Want to know how to center the value of a slider range input in your React project using NextJS and Tailwind

Can someone help me with the issue I'm having with the offset? Is there a way to link a value to the thumb? I've tried two methods, but one gives a significant offset and the other gives less. However, it seems more like a workaround than a solu ...

Customizing File Size and Dimensions in Form Submission with Dropzone.js in JavaScript

I am currently experimenting with some sample code for Dropzone.js and am interested in finding a way to include the file size as a form field when submitting: var KTFormsDropzoneJSDemos = { init: function(e) { new Dropzone("#kt_dropzonejs_exam ...

Prevent multiple requests on jQuery infinite scrolling

I have implemented pagination on my website where the next page is loaded automatically when the user reaches the bottom of the page. This is achieved by using jQuery's .on('scroll', this.detectScroll()) method which triggers a function to l ...

Refresh a page in AngularJS with only a single click

I am currently working with angularjs and I am trying to figure out how to refresh the page only once when it loads. Here is what I have attempted so far: <script> app.cp.register('userProfileController', function ($window) { debugger; ...

Struggling with resizing a webcam feed to fit the dimensions of the iframe container?

I'm facing a challenge embedding a camera feed into a webpage that needs to display manual focus and servo controls. The issue lies in the content scaling within the iframe. Even though the iframe boundary resizes according to the window's width ...

What is the most effective method to guarantee the creation of an object prior to navigating through the code that relies on it?

I recently encountered an issue with my code that utilizes fetch() to retrieve and convert .tiff images into an html5 canvas for display in a browser using tiff.js (https://github.com/seikichi/tiff.js/tree/master). While the functionality mostly works as i ...

When attempting to access API>19 on my Android device, I encountered an error in my Interface to Java that stated: "TypeError: Android.method is not a function."

Having my minimum API set to 16, everything seems to be working fine. However, debugging has become quite a challenge. I've read that after API 19, the Chrome debugger can be used. But when it comes to interfacing with Java code, I encounter the error ...

The <br/> tag is not functioning properly when retrieving text from a MySQL query

Currently, I am involved in a project that involves an A.I pushing text onto a MySQL database. Our goal is to display the ongoing writing process on a webpage. We are still actively working on this. We are retrieving the data and regularly checking the da ...

Client connected via Socket.io is not receiving the message event

My project was built using express-generator as the skeleton, so I had to implement a workaround which I found here. SERVER: io.on('connection', function(socket){ socket.on('message', function(msg){ io.emit('message', msg) ...

The for loop is throwing an error because the variable 'i' is not defined

I recently started using eslint and I'm in the process of updating my code to comply with my eslint configuration. module.exports = { env: { browser: true, commonjs: true, node: true, es2021: true, }, extends: 'eslint:recomm ...

The native MongoDB driver for Node.js seems to be failing to return a result when querying the '_id' field

I am encountering an issue when querying my database for a matching '_id'. The results are returning null, however in the mongodb shell, they are correct. Below is the code I am using: var collection = new mongodb.Collection(client, 'produc ...

What is causing Vue.js to lag slightly? Why is jQuery necessary to retrieve the current value?

I am encountering an issue with a select element <select id="filter" v-model="filter" @change="changeFilter"> <option value="all">All</option> <option value="one">One</option> <option value="two">Two</option> ...

Angular 4: Triggering a function by clicking a link with specific parameters

I am relatively new to working with Angular 4. I have an anchor tag that, when clicked, should redirect me to a link where I also need to pass parameters. I'm unsure if my current approach is correct or not. Above all, I really need guidance on how to ...

Packager freezing after running npm audit and the component directory is nonfunctional

Greetings, To begin with, I would like to acknowledge that this issue may have been addressed in previous posts, but despite following suggestions on Stack Overflow and GitHub, I am still facing two specific problems. Here are the issues I am encounterin ...

Troubleshooting Problems with Ruby Arrays, JavaScript, and JSON

I am facing a challenge with rendering a highcharts plugin in my rails application. I suspect it could be related to the sql queries fetching data from the database and converting them into a ruby array that the javascript code fails to interpret correctly ...