The code above seems to work. The names are added to the input box when you select them and removed from the input box when deselected (see demo below)
Can you explain in more detail what the issue is?
$('input:checkbox').change((e) => {
if ($(e.currentTarget).is(':checked')) {
var curVal = $('#name').val();
if (curVal) {
$('#name').val(curVal + ', ' + e.currentTarget.value);
} else {
$('#name').val(e.currentTarget.value);
}
} else if (!($(e.currentTarget).is(':checked'))) {
var curVal = $('#name').val().split(',');
var filteredVal = curVal.filter(el => el.trim() !== e.currentTarget.value)
$('#name').val(filteredVal.join(','));
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<input type="text" id="name"></input>
<br/>
<input type="checkbox" value="Alabama">Alabama<br/>
<input type="checkbox" value="Alaska">Alaska<br/>
<input type="checkbox" value="Arkansa">Arkansa<br/>