the process of attaching an event listener to every item in a list and accessing them individually

When you click on an item in the list, it will switch the .done class on and off.

<ul>
    <li class="bold red" random="23">Notebook <button>Delete</button></li>
    <li>Jello <button>Delete</button></li>
    <li>Spinach <button>Delete</button></li>
    <li>Rice <button>Delete</button></li>
    <li>Birthday Cake <button>Delete</button></li>
    <li>Candles <button>Delete</button></li>
</ul>

Answer №1

One way to achieve this is by adding a delegate event on the parent ul element and checking if the clicked element is an LI. If it is, you can toggle the class using classList.toggleClass.

Additionally, considering user behavior, it's advisable to handle double clicks as well. For instance, when a user adds a to-do item, they may want to select and copy it.

const ul = document.querySelector("ul.item-list");
ul.addEventListener('click', function(event) {
  if (event.target.tagName === 'LI')
    event.target.classList.toggle('done')
})
.done {
  text-decoration: line-through;
}
<ul class='item-list'>
  <li class="bold red" random="23">Notebook <button>Delete</button></li>
  <li>Jello <button>Delete</button></li>
  <li>Spinach <button>Delete</button></li>
  <li>Rice <button>Delete</button></li>
  <li>Birthday Cake <button>Delete</button></li>
  <li>Candles <button>Delete</button></li>
</ul>

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 functionality of opening a link in a new tab is not functioning properly in Internet Explorer

When I use window.open in jQuery to open a link in a new tab, it works perfectly for me in Chrome, Safari, and Firefox. However, I am facing an issue in IE10 where it does not work. $('.div').click(function() { $(this).target = "_blank"; ...

Enhancing Request JSON Body Validation in Next.js 14 API Handler

I'm currently in the process of developing an API for my iOS application using Next.js 14. I have managed to get it up and running successfully, however, I am facing some challenges when it comes to properly validating the body of a request. ESLint is ...

Error: The middleware function is not recognized | Guide to Transitioning to React Redux Firebase v3

After utilizing these packages for my project, I encountered an error in middleware composition while creating a new react app with create-react-app. Below are the packages I have included. Can someone please help me identify what is missing here? HELP I ...

Enhancing Json_Encode output with extra arrays

I'm facing some challenges with the json_encode output. To enhance the speed of our extensive dropdown navigation menu, I decided to store all the data in a json file that is updated daily and retrieved as needed. When generating the json using json_ ...

Using jQuery, JavaScript, and PHP, we can easily implement the functionality to disable the form and submit button

After submitting a form, I am trying to disable both the submit button and the form itself. However, every solution I have attempted either disables the form and button without submitting it or submits the form without disabling the button/form. This is t ...

Issue with Ng-style not functioning properly when setting background color

I am struggling to set the background of an element using a configuration object with ng-style. For some unknown reason, I can't seem to make it work and I'm finding it really perplexing. The element I'm attempting to configure: <div id ...

Resolved: Angular JS resolves circular dependencies

I encountered a Circular dependency issue while attempting to make a http.post call in a http interceptor: Circular dependency found: $http <- Ace <- authInterceptor <- $http <- $templateRequest <- $compile I understand the problem, but I ...

Encountering the "Local resource can't be loaded" error when attempting to link a MediaSource object as the content for an HTML5 video tag

I'm attempting to make this specific example function properly. Everything runs smoothly when I click the link, but I encounter an error when trying to download the HTML file onto my local machine and repeat the process. An error message pops up sayi ...

Tips for restricting users from inputting spaces into a form field using ReactJS

Within my application, I have developed a specialized component named QueryForm that serves the purpose of enabling search capabilities. The code snippet being outputted by this component is as follows: ...

The nightwatch.js script is halting operations once the test suite has been completed

Recently, I've implemented functional test automation using nightwatch.js. However, I encountered an issue where the test pauses after the test suite is completed, failing to end the process. Below is a snippet of the code: var afterSuite = function( ...

Printing all elements in a table (int) using the printf function

I am attempting to show all the values in the array 'tab', however, only the first 8 elements are being displayed and no more. int tab[] = {0,2,5,3,9,0,8,9,2,0,4,3,0}; printf("Size %d: \n", sizeof(tab)); for (int i=0; i<sizeof(tab); ...

Adjust the header image as you scroll

Currently, I have a static image in the header of the page. I'm looking to have the image change to a different one when half the page has been scrolled. Do I need to utilize JavaScript for this functionality or is it achievable with CSS alone? bo ...

Error: Attempting to access a property named '_updatedFibers' on an undefined object is not possible due to a TypeError

I encountered the following error: Uncaught TypeError: Cannot read properties of undefined (reading '_updatedFibers') at requestUpdateLane (react-dom.development.js:25411:23) at updateContainer (react-dom.development.js:28810:14) at ReactDOMHydra ...

Tips for enhancing image resolution when converting from canvas toDataURL

After grabbing an image from a DIV with HTML2CANVAS, I noticed that the image quality is disappointingly poor and pixelated. The resolution of the captured image is only 96 dpi. Is there a way to boost the resolution for a clearer, high-quality image? Cou ...

Checking the parameters passed to a function in Typescript: A step-by-step guide

Currently, I am working with Typescript and then transpiling my TS code into JavaScript. However, I have encountered an issue that I am struggling to resolve. The error message I am facing is as follows: Error Found in TypeScript on Line:2 - error TS230 ...

Updating state in React is not possible

I am having trouble updating my state (specifically with setCoords). The API request is returning a 200 status code and the elements I need are present: https://i.stack.imgur.com/a8QzN.png Below is the code I am working with: const App = () => { co ...

Learn how to dynamically disable unchecked checkboxes and apply specific CSS classes to checked checkboxes in Angular 12 with TypeScript

Currently, I am working on a project where I have successfully stored checkboxes dynamically using a JSON array of objects. However, I am facing an issue that requires your expertise. My goal is to allow the selection of only 5 checkboxes. Once this limit ...

Having trouble getting an HTML form to function with Ajax and PHP?

Seeking assistance from anyone who can lend a hand. I am delving into the complexities of Ajax, and I'm encountering issues where it seems like the script is being completely ignored or perhaps I'm just making a rookie mistake. Prior to display ...

Can anyone provide guidance on displaying a JS alert every time a tab in the slider is opened?

Utilizing the liquidslider script on my website, I have created a slider with the following HTML code: <div class="liquid-slider" id="slider-id"> <div> <h2 class="title">Slide 1</h2> // Content goes here ...

Exploring Symfony2 controller integration with Javascript arguments

I am currently working on a project with Symfony2 and I have a question regarding receiving arguments from a template in a controller. My goal is to take the value of the argument and store it in the database. The argument's value will be generated by ...