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

The Chrome developer tools are unable to locate the HttpRequest

While working in Python, I am utilizing the requests library to make a POST request to a specific URL. However, upon clicking the button, it seems that nothing is happening as per Chrome Developer Tools. No XHR requests are being made and no data is being ...

Creating TypeScript models from a JSON response in React components

My Angular 2 application retrieves a JSON string, and I am looking to map its values to a model. According to my understanding, a TypeScript model file is used to assist in mapping an HTTP Get response to an object - in this case, a class named 'Custo ...

Mastering the Art of Content Swapping in SPA

Hey there! I'm in the process of creating a webpage using tornado io and incorporating various graphs. To add some single page app magic, I decided to swap out content within a div like so: <div id="chartType"> Chart goes here</div> <a ...

Position the div alignment to the top within the table cell

I'm having trouble aligning the div under the header to the right. Here is my HTML code and a photo for reference: HTML Code: <td> <div class="schedule-content"> <div class="timestamp& ...

Enhancing a React modal to enable user input for updating state variables

Is there a way to utilize user input from the page to dynamically create elements in a React.js application? In my app.js file, I have defined some constants at the beginning for mock data: const book1 = [ {id: uuid(), content: 'reflections&apos ...

Is the float floating within another float?

This question may have a strange title, but I couldn't think of anything better to call it. So, here's the situation: I have a grid layout where I need my .search-wrapper to be 50% wide and floated to the right. In my demonstration on jsfiddle, ...

Extract the Date portion from a DateTime object in ASP.NET MVC

I am currently facing an issue with a property in my Model [Display(Name = "День рождения")] [DataType(DataType.Date)] public System.DateTime Birthday { get; set; } When trying to save the value to the database using AJAX, it is also ...

calculation of progress bar advancement

I want to make the progress bar in my game responsive to changes in the winning score. Currently, it moves from 0% to 100%, which is equivalent to 100 points - the default winning score. But I need the progress bar to adjust accordingly based on the user-i ...

In search of a regular expression for identifying any of several classes

In the process of upgrading all my websites, I decided to redesign them so that all URL's default to lower case. This meant changing all image names and paths to lower case as well, which was accomplished using regexes. However, this action inadverten ...

Encountering a strange issue when attempting to link app.js with mongodb

I recently set up MongoDB and the Compass, everything was working fine until I tried to connect MongoDB with my app.js. Now I'm getting a strange error message. Could anyone help me understand what this error means and how I can fix it? Unfortunately, ...

What is the best way to retrieve a value from a database and display it in a radio

Can you help me figure out how to implement this functionality? I have a database with a "boolean" field that stores 0 or 1 values. On an HTML page, there is a radioButton with options for OK or NO. I need to dynamically load the boolean value from the dat ...

When a user scrolls over an element with a data-attribute,

Looking to create a dynamic header effect on scroll? I have two headers, menu1 by default and menu2 hidden with display:none. In specific sections of my website, I've added a special attribute (data-ix="change-header") to trigger the header change. T ...

Dynamic data binding in AngularJS with dynamically generated views

The scenario: Currently, I am constructing a wizard in AngularJS that contains tabbed content with each tab representing a step in the wizard. These steps are fetched from the database through a Laravel controller and displayed using ng-repeat. Each tab c ...

Rendering data from an API using v-if

Could you help me change the tag that currently displays true or false? I want it to show "FREE" if the event is free and "PAID" if it's not. Check out the Eventbrite API here The response I'm receiving from data.events.is_free is in boolean fo ...

Postpone an automatic action in React

I'm currently working on adding a fade out animation to a page change in React, but I need to delay the click event before the page actually transitions. Here's the code snippet I have so far: export default function Modal({setTopOf}) { const ...

What is the reason behind opting for ng-style over style in AngularJS for applying CSS formatting?

Recently, I've delved into AngularJS and have been exploring its depths for a few days now. I'm curious about the use of ng-style in styling components within both static and dynamic webpages. Given that we already have the traditional style tag ...

Share JSON data across functions by calling a function

I am currently working on a project where I need to load JSON using a JavaScript function and then make the loaded JSON objects accessible to other functions in the same namespace. However, I have encountered some difficulties in achieving this. Even after ...

Optimizing Divs for Maximum Layout Efficiency

My current issue involves a series of divs that contain varying amounts of content. These divs are floated left and I am striving to align them seamlessly without any vertical space between them (except for the 10px margin that I have included). You can v ...

Increase the lag time for the execution of the on('data') function in Node.js

In my current function, it searches data from a database and performs an action with it. For the purpose of this demonstration, it simply increments a counter. exports.fullThreads = function(){ return new Promise((resolve, reject) => { MongoClien ...

Achieve maximum height with background images

Having trouble with responsiveness on my box containing a background image. I am aiming for the box to adjust its height based on the background image, as I have set the background-size attribute to auto. html: <div class="index_boxs" id="discover_win ...