I have the following form with bunch of checkboxes. I need to add or remove CaseIDs from the input if user checks or unchecks them.
The desired result is the unique caseIDs that are checked. Any ideas?
Thanks in advance.
Code Snippet
// This is what I have so far :
let selectedCases = $('input[id="save-info"]').filter(':checked').map(function() {
return this.value;
}).get();
let existingCases = $('#selected-cases').val();
$('#summary').html(selectedCases.join(","));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<form name="test-form" id="test-form" action="/delMe8.cfm" method="post">
<div class="col-12">
<label for="selected-cases" class="form-label">selected cases<span class="text-muted"> *</span></label>
<input type="text" id="selected-cases" name="selected-cases" class="form-control" value="3966382,4168801,4168802,4169839">
</div>
<hr class="my-4">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="3966382" checked>
<label class="form-check-label" for="save-info">Save CaseID : 3966382</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4029501">
<label class="form-check-label" for="save-info">Save CaseID : 4029501</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4168818">
<label class="form-check-label" for="save-info">Save CaseID : 4168818</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4168801" checked>
<label class="form-check-label" for="save-info">Save CaseID : 4168801</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4168802" checked>
<label class="form-check-label" for="save-info">Save CaseID : 4168802</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4168822">
<label class="form-check-label" for="save-info">Save CaseID : 4168822</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="3966388">
<label class="form-check-label" for="save-info">Save CaseID : 3966388</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4114087">
<label class="form-check-label" for="save-info">Save CaseID : 4114087</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="3966385">
<label class="form-check-label" for="save-info">Save CaseID : 3966385</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4169838">
<label class="form-check-label" for="save-info">Save CaseID : 4169838</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4169843">
<label class="form-check-label" for="save-info">Save CaseID : 4169843</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4168829">
<label class="form-check-label" for="save-info">Save CaseID : 4168829</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4168828">
<label class="form-check-label" for="save-info">Save CaseID : 4168828</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4168835">
<label class="form-check-label" for="save-info">Save CaseID : 4168835</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4169835">
<label class="form-check-label" for="save-info">Save CaseID : 4169835</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4169836">
<label class="form-check-label" for="save-info">Save CaseID : 4169836</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4169837">
<label class="form-check-label" for="save-info">Save CaseID : 4169837</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="4169839" checked>
<label class="form-check-label" for="save-info">Save CaseID : 4169839</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="3946200">
<label class="form-check-label" for="save-info">Save CaseID : 3946200</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="save-info" value="3946201">
<label class="form-check-label" for="save-info">Save CaseID : 3946201</label>
</div>
<button class="w-10 btn btn-primary btn-sm" type="submit">Save</button>
<div class="col-md-5">
<div id="summary">
selected-cases : <br/>
</div>
</div>
</form>
2
Answers
Here is a slightly edited version that will work in the expected way:
I also worked in the recommendations of @RokoC.Buljan: The event function for "input" events is now a separate function that is used to calculate the initial state.
So easy with vanilla JS…