You can associate a callback with several elements by specifying several selectors:
$(".field1, .field2, .field3").click(function() { return field1 + field2 + field3; });
If you need to perform certain actions depending on which element was clicked, another option would be to create a function that performs the actual calculation, and then calls it from each callback.
var calculate = function() { return field1 + field2 + field3; };
And then call this function when on every click:
$(".field1").click(function() { // Perform field1-specific logic calculate(); }); $(".field2").click(function() { // Perform field2-specific logic calculate(); }); // etc..
This means that you are not repeating yourself.
alexn source share