Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call multiple functions from single event with unobtrusive javascript

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.

like image 554
dan_vitch Avatar asked May 07 '13 21:05

dan_vitch


People also ask

Can we call multiple functions in JavaScript?

Yes, you can call two JS Function on one onClick. Use semicolon (';') between both the functions.

Can we call two functions onClick event?

Executing multiple functions with onclick is supported across all web browsers and for most HTML elements. It is a neat and concise method of multiple functions call.

How do I connect two functions in JavaScript?

You can't 'merge' functions as you describe them there, but what you can do is have one function be redefined to call both itself and a new function (before or after the original). var xyz = function(){ console. log('xyz'); }; var abc = function(){ console.

What is unobstrusive JavaScript?

In short, unobtrusive JavaScript is a way of writing JavaScript so that your site visitors are not shut out of your site for one of these reasons—even if your JavaScript is not working correctly for them, they should still be able to use your site, albeit at a more basic level.


1 Answers

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();
}
like image 84
Drew Avatar answered Sep 22 '22 00:09

Drew