0

I'm trying to figure out if there is a way to get all of the functions called from an onclick event.

The scenario is something like this:

HTML:

<div onclick="a(); b();">
     <a href="javascript();" onclick="c(event);>link</a>
</div>

JS:

var a = function() { console.log('a called'); };
var b = function() { console.log('b called'); };
var c = function(e) {
    if (e.call['a']) { e.call['a'].stopPropagation(); }
    console.log('b and c called, but not a.');
};

I want to use this to control which functions are stopped from propagating.

CJT3
  • 2,788
  • 7
  • 30
  • 45

2 Answers2

0

I'm afraid there are lots of incantations in this code. You should try and read more about the event flow, and to a lesser extent (because the subject is harder) about javascript execution contexts and possibly the javascript pseudo-protocol.

If I understand you correctly, you want a certain listener to be skipped if a previous listener has already been executed in the event flow - or, more probably, you want some content to be skipped, if some other content is executed first.

The user agent gives you no way to monitor the state of the execution of your listeners, so you need to manage that state yourself. Each listener must register that it has been called, so that next listeners can consult this state and adjust their logic. Or, as such an approach is likely to generate too much complexity (because of the tight coupling between listeners), you'd probably be better off writing some light rules framework, based on your needs.

You've asked a very specific, technical question, but do not hesitate to provide more context (the "why"), as the proper solution should be more a design-based one, rather than a technical-based one.

Elegie
  • 340
  • 1
  • 5
  • 16
-1

You could set global variables and set them when they are being called, for example:

aClicked = false;
bClicked = false;
function a() { aClicked = true; }
function b() { bClicked = true; }
var c = function() {
    if (!aClicked && bClicked) {
        console.log('b and c called, but not a.');
    }
};
Arko Elsenaar
  • 1,689
  • 3
  • 18
  • 33