Assuming that FORM contains INPUT, have the following listeners:
JavaScript
function formFirst(e) { }
function formLast(e) { }
function inputFirst(e) { }
function inputLast(e) { }
function middle(e) { }
document.getElementById('form').addEventListener('change',formFirst,true);
document.getElementById('form').addEventListener('change',formLast,false);
document.getElementById('input').addEventListener('change',inputFirst,true);
document.getElementById('input').addEventListener('change',inputLast,false);
Desired order of firing
formFirst() // normal - outer element, useCapture = true
inputFirst() // normal - triggering element, declared first
middle() // -- how to do this?
inputLast() // normal - triggering element, declared second
formLast() // normal - outer element, useCapture = false
Nature of problem and attempted solutions
Own code at FORM level, formFirst
, formLast
and middle
, but have no access to INPUT code, inputFirst
and inputLast
– although could add own listeners on the INPUT.
Attempt 1 modify formFirst()
to create and dispatch a new change Event
(would be ignored within formFirst
) that would call inputFirst()
, but have no way of stopping propagation to prevent inputLast()
being called subsequently.
Attempt 2 add middle
added as listener to INPUT, but cannot guarantee firing order of two listeners of same type and same useCapture.
Premise of Attempt 2 is incorrect – firing order is determined by declaration order within the target Element.
Here are the rules
non-target Element triggers with
useCapture=false
, starting at the outermost Element and working toward the target Elementa) if more than one
useCapture=true
triggers for same element, then order of declaration.at target Element, order of declaration, regardless of
useCapture
non-target Element triggers with
useCapture=false
, starting at the innermost Element and working away from the target Elementa) if more than one
useCapture=false
triggers for same Element, then order of declaration.
Advertisement
Answer
I think that this answers just your question. feel free to commentcontact me for more info.
—– edit ——
OK, I just played with it a little as promised, and I found a very simple solution:
<script type="text/javascript">
function formFirst(e) { alert(1); }
function formLast(e) { alert(5); }
function inputFirst(e) { alert(2); }
function inputLast(e) { alert(4); }
function middle(e) { alert(3); }
function init(){
document.getElementById('form').addEventListener('change',formFirst,true);
document.getElementById('form').addEventListener('change',formLast,false);
document.getElementById('input').addEventListener('change',inputFirst,true);
document.getElementById('input').addEventListener('change',middle,true);
/*** alternative to last tow lines
document.getElementById('input').addEventListener('change',function(){inputFirst();middle();},true);
**/
document.getElementById('input').addEventListener('change',inputLast,false);
}
</script>
<body onload="init();">
<form id="form">
<input type="text" id="input" /> <br />
</form>
</body>
notice:
- I put the addEventListener part into an init function, so I can call it after the page is loaded and the element are already exist.
- I have run this just on chrome. So I don’t want to guarantee you things about other browsers.
An alternative is writing the event handling on your own. here is an example for that. relaying on this article.
JavaScript147471<script type="text/javascript">
2
3function formFirst(e) { alert(1); }
4function formLast(e) { alert(5); }
5function inputFirst(e) { alert(2); }
6function inputLast(e) { alert(4); }
7function middle(e) { alert(3); }
8
9function init(){
10
11//create event
12myHandler = new Event();
13
14//add handler
15myHandler.addHandler(formFirst);
16myHandler.addHandler(inputFirst);
17myHandler.addHandler(middle);
18myHandler.addHandler(inputLast);
19myHandler.addHandler(formLast);
20
21//regiser one listener on some object
22document.getElementById('input').addEventListener('change',function(){myHandler.execute();},true);
23}
24
25
26function Event(){
27this.eventHandlers = new Array();
28}
29
30Event.prototype.addHandler = function(eventHandler){
31this.eventHandlers.push(eventHandler);
32}
33
34Event.prototype.execute = function(){
35for(var i = 0; i < this.eventHandlers.length; i++){
36this.eventHandlers[i]();
37}
38}
39
40
41</script>
42<body onload="init();">
43<form id="form">
44<input type="text" id="input" /> <br />
45</form>
46</body>
47