0

right now, I have an event handler that looks like this

   $('#nextMonth, #prevMonth').click(function() {
      // execute code
   });

but trying to make it look something more like this, but not sure how to write it in a way that works

   $('#nextMonth, #prevMonth').click || $('.yearSelect).change(function() {
      // execute code
   });

so basically, if #nextMonth / #prevMonth is clicked, run the function, or if $('.yearSelect') is changed (a dropdown menu), then execute the code in the function. What would be the correct way to write this?

1 Answer 1

2

To execute common code from different event handlers, you can move the common code in javascript / jquery function and call it from event handlers

$('#nextMonth, #prevMonth').click(function() {
  commonExecution();
});

$('.yearSelect').change(function() {
  commonExecution();
});

function commonExecution() { 
  // execute code
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Kaddath OP need change event on different element and click event on different element
I need to drink another coffee it seems :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.