Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an input have more than one event?

I have a button and a select in my form. The button has onclick event while the select has onchange event. How to make the select has both onclick and onchange event?

This is the script:

$('#i_btn').on('click', function(){
     alert('Event 1');
});

$('#i_sel').on('change', function(){
     alert('Event 2');
});

When the select is changed, it should do alert('Event 1'); alert('Event 2');. I do not want to rewrite the onclick code in onchange

like image 957
Shota Avatar asked Nov 25 '25 22:11

Shota


1 Answers

Write your events as separate functions :

$('#i_btn').on('click', event1);
$('#i_sel')
     .on('change', event1)
     .on('change', event2);

function event1(){
     alert('Event 1');
}

function event2(){
     alert('Event 2');
}
like image 72
Mik Avatar answered Nov 27 '25 11:11

Mik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!