I am trying to update the value of an input field based on what options the user selects. So, if they select an option 'education', it would add it to the input value field. If they select that same option again it would remove. They can add as many or as few options as they want. I'm a little stuck with two things:
- If they add more than one option, is it possible to separate these values by a comma?
- If they select the same option again but added others, it would remove only that option and keep the rest?
Any help? This is what I have so far. I have set up a jsFiddle that might help? Thanks.
https://jsfiddle.net/w1x9Lpho/
$(document).on('click touchstart', '.project-library-filters-container ul.project-sectors-list li a', function(e) {
$(this).each(function() {
$(this).parent('li').addClass('active');
if ($(this).parent('li').is('active')) {
var dataName = '';
$('input[name=project_sectors]').val($('input[name=project_sectors]').val() + dataName).serialize();
} else {
var dataName = $(this).data('name');
$('input[name=project_sectors]').val($('input[name=project_sectors]').val() + dataName).serialize();
}
});
e.preventDefault();
});