Update the row striping pattern once an element has been removed

I've been working on creating a todo-list and implemented row striping. Everything was going smoothly until I realized that when I delete a task, the row striping does not update as intended. Instead of changing to white, white, beige after a deletion, it should update to white, beige, white.
I've tried multiple solutions but haven't been able to make it work. Admittedly, I'm new to working with row striping and could use some guidance.
Is there a way to achieve this?

Here's the code I have so far:

loadEvents();

function loadEvents() {
  document.querySelector('form').addEventListener('submit', submit);
  document.querySelector('ul').addEventListener('click', deleteOrTick);
}

function submit(a) {
  a.preventDefault();
  let input = document.querySelector('input');
  if (input.value != '') {
    addTask(input.value);
  }
  input.value = '';
}

function addTask(task) {
  let ul = document.querySelector('ul');
  let li = document.createElement('li');
  li.innerHTML = `<div class="input-group mb-3 row"><div class="col-11"><label>${task}</label></div>
    <span class="delete">x</span></div>`;
  ul.appendChild(li);
  document.querySelector('.allToDos').style.display = 'block';
}

function deleteOrTick(a) {
  if (a.target.className == 'delete') {
    deleteTask(a);
  }
}

function deleteTask(a) {
  let remove = a.target.parentNode;
  let parentNode = remove.parentNode;
  parentNode.removeChild(remove);
  event.stopPropagation();
}
ul {
  list-style-type: none;
}

li {
  font-size: 1.3em;
  color: #2f4f4f;
}

.todo li:nth-child(2n) {
  background: #e0d9c3;
}

.todo {
  width: 500px;
}

.delete {
  cursor: pointer;
}
<div class="container">
  <form action="index.html" method="post">
    <div class="heading">
      <h1 class="header">ToDo-List</h1>
      <p class="intro">Do what you do</p>
    </div>

    <div class="input-group mb-3">
      <input type="text" class="form-control" name="task" placeholder="Add a todo" aria-describedby="basic-addon2">
      <div class="input-group-append">
        <button class="btn btn-outline-secondary" type="submit">Add</button>
      </div>
    </div>
  </form>
</div>
<div class="container">
  <div class="allToDos ">
    <ul class="todo">
      <li>
        <div class="input-group mb-3 row">
          <div class="col-11">
            <label>hi was geht ab</label>
          </div>
          <span class="delete">x</span>
        </div>
      </li>
    </ul>
  </div>
</div>

Answer №1

deleteTask can be implemented as follows:

function deleteTask(event) {
    let target = event.target.parentNode;
    let parent = target.parentNode;
    target.parentNode.remove();
    event.stopPropagation();
}

Answer №2

document.querySelector('li').addEventListener('click',removeOrComplete); // excluding ul

function removeItem(item){
    let deleteItem = item.currentTarget;
    let parentElement = deleteItem.parentNode;
    parentElement.removeChild(deleteItem);
    item.stopPropagation(); // excluding event
}

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

Guide on implementing a bootstrap loading spinner during the process of retrieving data from an external api

Is there a way to use the bootstrap load spinner to mask the delayed information retrieval from a url and enhance the website's interactivity? ...

React Native: Issue with Higher Order Component when utilizing the `onLayout` prop

The functionality of this component involves rendering the Placeholder. Once it acquires its layout, this higher-order component (HOC) updates the state and invokes the child component with the values. Everything seems to be working fine up to this point. ...

Localhost is unable to process AngularJS routes containing a dot in the URL

When using the route provider and setting this specific route: .when('/:name/:id', { It successfully navigates to my desired path and executes the code when I enter: https://localhost.myapp.com:9000/Paul/123 However, it fails to work with this ...

Displaying Multiple HighCharts on a single AngularJS page

As a beginner with HighCharts, I am working on adding two Highcharts to the same page that will access the same data source but display different pieces of data for each graph. For example, the categories will remain constant while the series[] will vary. ...

Examining the execution of a function/method when a click event occurs within a functional component

It took me a while to realize that testing functional components with vue-test-utils can present some challenges In my case, I am utilizing Bootstrap-Vue's B-Button with a @click event that calls a function/method. When I attempt to test whether the ...

Z-index behaving abnormally

I've been working on developing a website and one of the key features I'm trying to implement is a slideout menu that slides horizontally when the mouse hovers over it. To make development easier, I initially set the menu to be fully extended by ...

Managing date validation for a three-part field using AngularJS

I am currently working on validating a three-part date field using AngularJS. I have managed to create a custom validation function, but I am struggling with determining how the fields should update each other's status. How can I ensure that all thre ...

AngularJS - Custom directive to extract a property value from an object

Currently, I am using the following for loop to retrieve the parent category: angular.forEach(queryTicketCategories, function(category) { if(category.id === $scope.ticketCategory.parentId) { $scope.parent = category; } }); I am looking fo ...

I require assistance in implementing a button for executing this specific HTML code

Can someone assist me in embedding my HTML code into a button so that when the button is clicked, my code executes? function loadProgressBar() { var bar = document.getElementById('progressBar'); var status = document.getElementById(&ap ...

Html elements remain stationary and do not shift positions

I am attempting to customize a web page by repositioning an input text element. Below is the code I'm using: <body> <h1>Shopping List</h1> <input id="search" type="search"> <input id='newItem' type="t ...

Updates to the visibility of sides on ThreeJS materials

When viewed from the back, the side is hidden as desired, but I am struggling to determine if it is visible from the renderer or camera. new THREE.MeshBasicMaterial({ map: new, THREE.TextureLoader().load('image.jpg'), side: THREE. ...

Creating a custom Chrome extension with the ability to modify the pop-up window instead of the web page

Is there a way to modify the content inside my extension's pop-up without affecting the web page being viewed by the user? Also, how can I ensure that my dropdown list functions correctly? I have two dropdown lists where selecting an option from the ...

checkbox inspired by the design of the iPhone

I'm looking to create a custom checkbox! <label id="sliderLabel"> <input type="checkbox" /> <span id="slider"> <span id="sliderOn">SELECTED</span> <span id="sliderOff">SELECT</span> ...

Hide jquery scroll bar

I am currently working on a WordPress plugin with the Twenty Thirteen theme. My goal is to display a modal when a div is clicked, and at that time I want to hide the scrollbar on the body of the page. Despite trying the following code snippet, it doesn&ap ...

Validating Credit Card Numbers with Spaces

Currently, I am in the process of creating a credit card form that requires validation using checkValidity to match the specific card pattern that is dynamically added to the input field. For example, if a user enters a Mastercard number such as 545454545 ...

Issue: The module '[object Object]' could not be located

const express = require('express'); const app = express(); const jade = require('jade'); const path = require('path'); const server = require('http').createServer(app); const io = require('socket.io').liste ...

If tall, then the height is set to 100%; if wide, then the width is set

My image-generating algorithm produces images of various sizes, some tall and some wide. How can I ensure that the images always fit perfectly within their parent container? They should be 100% height if they are tall and 100% width if they are wide. .im ...

Utilizing the .add() method in Firebase Cloud Firestore for working with nested

In the documentation for Firebase, it mentions updating nested objects. You can find out more about this here: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects Here is my data structure: let ref = db.collect ...

The route handler for app.get('/') in Express is not returning the input data as expected

I have multiple routes set up, and they are all functioning properly except for the app.get('/') route. When I navigate to 'localhost:3000/', nothing is being displayed in the backend console or on the frontend. The Home component is su ...

Limiting Firebase communication to Electron Application

Is there a way to restrict access to a Firebase realtime database or any other database from an Electron application? I understand that you can restrict access by specifying the server your website is hosted on, but since Electron is local, are there any o ...