mercredi 21 décembre 2016

How to create a dynamic jQuery string of checkbox id's

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);
    })
})




Aucun commentaire:

Enregistrer un commentaire