call multiple functions from single event with unobtrusive javascript
Asked Answered
V

5

6

I have an input element

<select id="test"></select>

on change I want to call multiple functions

$('#test').change(function1, function2);

functions for now are just alerts for now

var function1 = function(){alert('a');};
var function2 = function(){alert('b');};

Only the second function is being called. I know this because of alerts and brake points. I know one way to correct this would be to call function1, and function2 from another function, but I would like to avoid that.

Vincennes answered 7/5, 2013 at 21:7 Comment(0)
E
6

I prefer not to use anonymous functions so I would create a new function and place all work inside it.

$('#test').change(onSelectChange);

var onSelectChange = function() {
    function1();
    function2();
}
Ernie answered 7/5, 2013 at 23:24 Comment(0)
T
5

You can always chain them:

$('#test').change(function1).change(function2);
Telemachus answered 7/5, 2013 at 21:12 Comment(0)
A
0

You can do this -

$('#test').change(function1);
$('#test').change(function2);
Appetency answered 7/5, 2013 at 21:9 Comment(0)
H
0

You can do it like this:

$('#test').change(function(){function1(); function2();});
Haire answered 7/5, 2013 at 21:10 Comment(0)
S
0

Why not try something like:

$('#test').change(function(){
   function1();
   function2();
};
Schnorr answered 7/5, 2013 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.