You can have check boxes with the same name in a form, allowing users to select more than one. Many developers know or have realized that by design & definition $(field-selector).val() returns the value of the first field matched. How do we get all the values? The following jQuery plugin will do just that for you. It returns a list of comma separated values for all selected check boxes. Feel free to change the value separator to suit your needs, by changing .join(',') to .join('|'), for example.
- (function($)
- {
- $.fn.valList = function()
- {
- return $(this).map(function() { return $(this).val(); }).get().join(',');
- };
- })(jQuery);
Usage: very similar to the way .val() is used:
var myvals = $(selector).valList();