Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having two click event handlers in two different js files.
say in file1:
$('.test').live('click',function() {
//action 1
}

file 2 :
$('.test').live('click',function() {
//action 2
}

What I have tried:

which will execute first .? My assumption is , it will be executed based on the order of js file loaded. but i am not sure . can any one clear my doubt ??
Posted
Updated 23-Feb-17 9:01am
Comments
[no name] 23-Feb-17 12:50pm    
Why don't you try it and see which fires first? Would have taken you less time to find out than posting here only to be told to try it first.
moyna coder 23-Feb-17 23:43pm    
Its not that i have n't tried .. I tried , it seems execution differs some time. I just wants to confirm .
Richard Deeming 23-Feb-17 14:55pm    
.live() has been deprecated since jQuery v1.7; if you're using 1.7 or later, you should be using .on()[^] instead.

First File reference will be executed first.

tested as below

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">
    </script>
    <script src="Scripts/JavaScript1.js"></script>
    <script src="Scripts/JavaScript2.js"></script> 
</head>
 
<body >
   <button class="test"> click to test </button>  
</body>
</html>
 
</html> 


File 1
$('.test').live('click', function () {
    alert('file1'); // shown 
});


File 2

$('.test').live('click', function () {
    alert('file2');
});
 
Share this answer
 
Whilst the event handlers currently seem to execute in the order in which they were registered (see Solution #1[^]), that behaviour doesn't seem to be documented anywhere.

That means it's an internal implementation detail, and could change in a future version of jQuery, or possibly even with a browser update.

Relying on different event handlers executing in a specific order is a "code smell[^]". If you really need a set of actions to execute in a specific order, then user a single handler, and invoke the actions in the required order.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900