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
x
4
1
function theFunction(event) {
2
event.preventDefault();
3
console.log("test");
4
}
JavaScript
1
23
23
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<ul class="dropdown-menu" id="userlist">
3
<li class="list-group-item border-0 py-2" onclick="theFunction(event)" id="first-wrapper">
4
<input class="form-check-input me-1" type="checkbox" value="first" id="first">
5
<label for="first">First checkbox</label>
6
</li>
7
<li class="list-group-item border-0 py-2" onclick="theFunction(event)" id="second-wrapper">
8
<input class="form-check-input me-1" type="checkbox" value="second" id="second">
9
<label for="second">Second checkbox</label>
10
</li>
11
<li class="list-group-item border-0 py-2" onclick="theFunction(event)" id="third-wrapper">
12
<input class="form-check-input me-1" type="checkbox" value="third" id="third">
13
<label for="third">Third checkbox</label>
14
</li>
15
<li class="list-group-item border-0 py-2" onclick="theFunction(event)" id="fourth-wrapper">
16
<input class="form-check-input me-1" type="checkbox" value="fourth" id="fourth">
17
<label for="fourth">Fourth checkbox</label>
18
</li>
19
<li class="list-group-item border-0 py-2" onclick="theFunction(event)" id="fifth-wrapper">
20
<input class="form-check-input me-1" type="checkbox" value="fifth" id="fifth">
21
<label for="fifth">Fifth checkbox</label>
22
</li>
23
</ul>
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
1
6
1
const created = [];
2
function theFunction(event) {
3
const index = created.indexOf(event.target.value);
4
index === -1 ? created.push(event.target.value) : created.splice(index, 1);
5
console.log(created);
6
}
JavaScript
1
7
1
li {
2
display: flex;
3
}
4
5
label {
6
flex: 1;
7
}
JavaScript
1
22
22
1
<ul class="dropdown-menu" id="userlist">
2
<li class="list-group-item border-0 py-2" id="first-wrapper">
3
<input class="form-check-input me-1" type="checkbox" value="first" id="first" onclick="theFunction(event)">
4
<label for="first">First checkbox</label>
5
</li>
6
<li class="list-group-item border-0 py-2" id="second-wrapper">
7
<input class="form-check-input me-1" type="checkbox" value="second" id="second" onclick="theFunction(event)">
8
<label for="second">Second checkbox</label>
9
</li>
10
<li class="list-group-item border-0 py-2" id="third-wrapper">
11
<input class="form-check-input me-1" type="checkbox" value="third" id="third" onclick="theFunction(event)">
12
<label for="third">Third checkbox</label>
13
</li>
14
<li class="list-group-item border-0 py-2" id="fourth-wrapper">
15
<input class="form-check-input me-1" type="checkbox" value="fourth" id="fourth" onclick="theFunction(event)">
16
<label for="fourth">Fourth checkbox</label>
17
</li>
18
<li class="list-group-item border-0 py-2" id="fifth-wrapper">
19
<input class="form-check-input me-1" type="checkbox" value="fifth" id="fifth" onclick="theFunction(event)">
20
<label for="fifth">Fifth checkbox</label>
21
</li>
22
</ul>