Im at my wits end with this one.
When i click a checkbox, I want add the ID of the checkbox to a comma separated string in an input below. I have this working however, what I can not do is remove the ID and its comma if it already exists in the input field (checking and unchecking).
Simple form.
<form>
<input class="iteminput" type="checkbox" value="1" id="1" name="<?php echo $title; ?>">
<input class="iteminput" type="checkbox" value="2" id="2" name="<?php echo $title; ?>">
<input class="iteminput" type="checkbox" value="3" id="3" name="<?php echo $title; ?>">
<!-- Note that this field may or may not have an existing string of ID's (1,2,3) from previous saves -->
<input type="text" id="excludelist" value="<?php echo $saved-string; ?>">
</form>
jQuery(document).ready(function(){
jQuery('.iteminput').on('click', function(){
var id = jQuery(this).attr('ID');
var string = jQuery('#excludelist').val();
var newstring = string + id + ',';
jQuery('#excludelist').val(newstring);
})
})
javascriptat Question where you tried removing the string frominput.value? Why do you concatenate the string if you can check if the string is already present within the.valuebefore concatenating the string?