Reputation: 7076
I have not found any answer. So asking.
My HTML code
<button id="myButton">Click Me</button>
<select id="mySelect">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
My JS
$("#myButton").on("click", function(){
alert("Hi");
});
$("#mySelect").on("change", function(){
alert("Hi");
});
So in both events my function do the same thing. How can I combine these two events and type my function there? Is it possible?
I heard about trigger. I can use that. But want to know if there is any way to merge these events.
Upvotes: 0
Views: 70
Reputation: 446
You can use javascript function
<script>
function clickme(){
alert("Hi");
}
</script>
<button id="myButton" onclick="clickme()">Click Me</button>
<select id="mySelect" onchange="clickme()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Upvotes: 0
Reputation: 318352
Use one function, and reference it in the event handlers
$("#myButton").on("click", function_name);
$("#mySelect").on("change", function_name);
function function_name() {
// code for both handlers goes here
}
Upvotes: 6