javascript - Need to append / delete id into hidden field on "change" -


i ve got numérous checkboxes in page, each time click checkbox, id im interested in , fill hidden form. script working if unclick checkbox, should delete id hidden field (right appends again id in hidden field). how can writte such script ? here's code far:

$(document).ready(function() {     var string = "";     $('.checkbox').on('change', function() {     var subscription_id = $(this).parent().attr('id');     if(string == "") {       string = subscription_id;     } else {       string = string + ", " + subscription_id;     }     $('#select_players').val(string);     $('#subscription_ids_export').val(string);   }) }) 

you can simplify retrieving subscription_id values checkboxes checked instead of adding/removing values each time.

try this:

$('.checkbox').on('change', function() {     var subscriptionids = $('.checkbox:checked').map(function() {         return $(this).parent().prop('id');     }).get().join(',');     $('#select_players').val(subscriptionids);     $('#subscription_ids_export').val(subscriptionids); }) 

example fiddle


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -