You do not need to be so explicit. Especially if we are dealing with an identifier, there is nothing quicker to request that the identifiers have to be unique in the markup. So:
'surnames givennames towns givennames years types'.split(/\s/).forEach(function( id ) { $('ul#filters-' + id + 'li a').click(function( myid ) { return function( e ) { var myValue = $(this).data( myid.replace(/s$/, '') ); $('form#formHidden').append( '<input type="hidden" name="query' + myid + '" value="' + myValue + '" />' ); $('form#formHidden').submit(); return false; }; }(id)); });
You do not need to explicitly call the .preventDefault() of the event object when returning false from the event handler.
This should give you a general idea of how to do this. I tried to distract some of your redundant variable names.
Even this solution is probably too specific. You can probably use a type selector
$('#sidebar li a').each(function( _, anchor) {
if there is no other unordered list in #sidebar , this will also remove the explicit loop over the string list.
jAndy source share