Slinky
Slinky

Reputation: 5832

Multi-selector JQuery Handler

In the interest of not duplicating code, I was hoping there is a way to encapsulate both of these selectors into 1

The 2 selectors are as follows:

 //Radio Button Group
 $("#ContactMethod").on("change", function(e) {

    //SAME CODE
 });

 //Submit button
  $(".btn").on("click", function(e) {}

        //SAME CODE
  );

Upvotes: 0

Views: 33

Answers (1)

Jack
Jack

Reputation: 8941

Have them call the same function.

$("#ContactMethod").on("change", mySameCode);
$(".btn").on("click", mySameCode);

function mySameCode(){
   //some code
}

Upvotes: 2

Related Questions