Strikeout list on click of a mouse

Is there a way to apply strikethrough formatting to text with just a mouse click? The CSS for lists is beyond the form field margin. I've tried multiple methods without success. No matter how many times I change the code, I can't seem to get it right. I've extensively searched the site for a solution to no avail. I attempted to troubleshoot but couldn't find anything to resolve this issue

$(document).ready(function() {

  $('form').on('submit', function(e) {
    e.preventDefault();

    const taskList = $('#task-list').val();
    const newItem = $('<li></li>');

    $(`<li>${taskList}</li>`).appendTo(newItem);
    $(newItem).appendTo('ul');
    $("li").click(function() {
      $(this).addClass("striked-through");
    });
    $('#task-list').val("");
  });
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: Roboto, sans-serif;
}

.container {
  margin: 100px auto;
  max-width: 640px;
  width: 100%;
}

form {
  margin-top: 40px;
}

input,
textarea,
button {
  display: block;
  padding: 8px;
  margin-bottom: 8px;
  width: 100%;
  resize: none;
}

button {
  background-color: #2e9ccc;
  border: none;
  text-transform: uppercase;
  color: #fff;
  font-weight: bold;
  font-size: 18px;
  font-style: italic;
  cursor: pointer;
}

button:hover {
  background-color: #18a9e9;
}

input:focus,
textarea:focus {
  outline-color: #18a9e9;
}

ul {
  text-decoration: line-through;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>jQuery</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="main.css">
</head>

<body>
  <div class="container">
    <header>
      <h1>Task List</h1>
    </header>
    <form id="form">
      <input type="text" id="task-list" required /> <button type="submit">Add Task</button>
    </form>
    <div class="list">
      <ul>
      </ul>
    </div>
  </div>
  <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
  <script src="./main.js"></script>
</body>

</html>

Answer โ„–1

Noticed some bugs and left comments at the problematic areas:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>jQuery</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="main.css" />
  </head>

  <body>
    <div class="container">
      <header>
        <h1>Lista de tarefas</h1>
      </header>
      <form id="formulario">
        <input type="text" id="lista-de-tarefa" required />
        <button type="submit">Cadastrar</button>
      </form>
      <div class="lista">
        <ul></ul>
      </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
    <script src="./main.js"></script>

    <script>
      $(document).ready(function () {
        //๐Ÿ‘‡remove from the submit, otherwise it will add a new event every time, causing problems
        //changed to on to make it target dynamic added elements
        $("ul").on("click", "li", function () {
          $(this).toggleClass("riscado"); //changed to toggle to be able to remove on click
        });

        $("form").on("submit", function (e) {
          e.preventDefault();

          const listaDeTarefa = $("#lista-de-tarefa").val();

          $(`<li>${listaDeTarefa}</li>`).appendTo("ul");

          $("#lista-de-tarefa").val("");
        });
      });
    </script>

    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: Roboto, sans-serif;
      }

      .container {
        margin: 100px auto;
        max-width: 640px;
        width: 100%;
      }

      form {
        margin-top: 40px;
      }

      input,
      textarea,
      button {
        display: block;
        padding: 8px;
        margin-bottom: 8px;
        width: 100%;
        resize: none;
      }

      button {
        background-color: #2e9ccc;
        border: none;
        text-transform: uppercase;
        color: #fff;
        font-weight: bold;
        font-size: 18px;
        font-style: italic;
        cursor: pointer;
      }

      button:hover {
        background-color: #18a9e9;
      }

      input:focus,
      textarea:focus {
        outline-color: #18a9e9;
      }

      li {
        cursor: pointer; /* ๐Ÿ‘ˆlooks better this cursor where clicking does something */
        user-select: none; /* ๐Ÿ‘ˆprevent selection for better UX */
      }

      .riscado { /* ๐Ÿ‘ˆ changed ul to the class */
        text-decoration: line-through;
      }
    </style>
  </body>
</html>

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

Sending authorization codes to web pages

I have developed a user authentication system that generates an access token after successful login. The challenge I am facing is passing this token to different pages as a header parameter in order to grant access to those pages. module.exports.authen ...

Set values for all variables that begin with a specific string

[WARNING! Proceed with caution] One of my most recent coding dilemmas involves creating a JavaScript script that can identify specific surveys. These surveys use variables that share a common string followed by a random number, like this: variableName_46 ...

Intermittent loading issues with Highcharts using AJAX requests

We've encountered intermittent errors when using Highcharts. Despite our efforts, we can't seem to pinpoint the cause of these issues in Chrome: Uncaught Highcharts error #16: www.highcharts.com/errors/16 VM210:16 HighCharts was already loaded V ...

Understanding information in Backbone.js

I have multiple backbone models with nested sub-models. Here's how I've approached the solution: Models.Base = Backbone.Model.extend ({ relatedModels: {}, /** * Parses data based on the list of related models. * * @since ...

Issue with Videogular: hh:mm:ss Date filter not functioning properly

I am currently working on integrating Videogular into my audio player application. Below are the settings provided in the example code on this particular page: <vg-time-display>{{ currentTime | date:'mm:ss' }}</vg-time-display> ...

Eliminate a descendant of a juvenile by using the identification of that specific juvenile

Here is the current structure I'm working with: I want to figure out how to eliminate any field that has the id 3Q41X2tKUMUmiDjXL1BJon70l8n2 from all subjects. Is there a way to achieve this efficiently? admin.database().ref('UsersBySubjects&ap ...

Issue with triggering a modal while simultaneously closing another one

Let's consider this situation. I currently have a modal called Modal A, which has 2 buttons in the footer: Save and Close. When I click on the "Save" button, my intention is to close Modal A and open Modal B. The code that achieves this functionality ...

Using a custom module in node.js to retrieve data from a mysql database

How can I retrieve select query results? I am currently getting empty or null responses. Despite attempting callback logic and using the callback function as suggested in some articles, I have yet to have any success. All of the code for my Custom Module ...

Error in React UseTable: Attempting to read properties of an undefined object (specifically trying to use a forEach loop)

https://i.stack.imgur.com/ZSLSN.pngHaving an issue here that I need some quick help with! Whenever I attempt to render data into a React table, I encounter the error mentioned above. The data is fetched from an API using Axios. Let's take a look at t ...

Transform the JSON object into a different JSON format

I am in the process of restructuring the JSON data which is currently organized by categories, with each category containing multiple locations. Each location consists of latitude/longitude coordinates and an area code: { "cat1":[ {"location":{ ...

Retrieve data from the database and automatically populate all text fields when the dropdown value is modified

I need assistance with populating all textbox values based on the dropdown selection. The dropdown values are fetched using an SQL query. Here is the HTML Code: <select name="name" ID="name" class="form-control"> <opt ...

Angular Tip: Display data in a dropdown using a select element

When I have two select elements and want to display values if they exist: page.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app ...

How to Delete Decimal Points from a String Using Regular Expressions

I am searching for a way in JavaScript, such as REGEX, to eliminate decimal numbers from a string. Here is an example input: Mazda 6 2.0 Skyactiv-G Wagon 2018 - Startstop.sk - TEST Mercedes C63 AMG Sound REVVING 6,3l V8 And here is the expected output: M ...

The onProgress event of the XMLHttpRequest is triggered exclusively upon completion of the file upload

I have a situation with my AJAX code where the file upload progress is not being accurately tracked. The file uploads correctly to the server (node express), but the onProgress event is only triggered at the end of the upload when all bytes are downloaded, ...

Retrieving data from a subcollection in a cloud firestore database does not yield any results

In my Next.js application, I am utilizing Cloud Firestore database to store user documents. The structure of the collection path is as follows: collection "userDocs" โ””โ”€ document "userEmail" โ””โ”€ collection "docs" โ””โ”€ document "document ...

Prevent Cordova from shrinking the view when focusing on an input

Currently working on developing an app using Cordova/Phonegap (v6.5.0). Platforms where I've installed the app: - IOS (issue identified) - Android (issue identified) - Browser (no issue found) I am facing a known problem without a suitabl ...

In Selenium IDE, the command to locate elements by ID works smoothly, but when using Java, the same

I encountered a challenge while trying to locate a dropdown box. Despite being able to make it work using an absolute path, I struggled with identifying the element by its xpath or id. It's quite frustrating and I'm struggling to find a solution. ...

The Context API leaves me feeling lost and confused

I am currently utilizing Auth0 for user sign up. My goal is to extract the user id listed under sub:value, and then add it to my database to associate it with a user's post. To achieve this, I am attempting to utilize a Context API to retrieve the use ...

The URI entered is not valid: The parsing of the hostname failed. Electron Builder

Having an issue while trying to build an electron app using squirrel, even though the iconUrl is valid. Here is my package.json configuration: "squirrelWindows": { "iconUrl": "http://95.85.39.111:5005/skylog.ico" }, Error message received: An unhand ...

Error: Unable to process reduction on ships - function "reduce" is not functional

I created a boat visualization tool using a specific API. The API responds with a JSON that I then inject into a table. The issue: At times during the day, I observed the application would abruptly stop functioning and throw an error: Unhandled Rejection ...