Skip to content
Advertisement

Add or Remove clicked Item from the Array list

I want to create an Array of selected/checked item and use it further.

Below is my Basic HTML and JS code (external JS).

  • If item checked, that item should be added to the created Array
  • If item unchecked, that item should be removed from the created Array

Note: I tried this solution too, but it’s not working like i wanted. (How can I remove a specific item from an array?)

My JS and HTML Code:

JavaScript
JavaScript

Advertisement

Answer

Just push the element to array, if the element doesnot exist in array.

If the element already exist, remove it from array using Array.splice

I have moved the click even from the li to the input.

Also I have used flex display for the elements, so that the label can use the remaining space in the li

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement