1

I'm using Remodal Javascript lib to handle with modal windows. The point is that the library has handlers for custom events, e.g. the following javascript

$(document).on('opening', '.remodal', function (e) {
   console.log('opening');
});

will be triggered when a modal window is open by clicking on another DOM element.

<a data-remodal-target="modal-window-id" id="trigger">CLICK ME TO OPEN MODAL</a>

<div class="remodal data-remodal-id="modal-window-id>MODAL CONTENT</div>

Now, If I add in a javascript file also the following call to a click event

$(document).on('click', '#trigger', function (e) {
   console.log('trigger clicked');
});

regardless of the position of the previous script (i.e. before or after the other), I see that the opening event is fired before. I know, in Javascript there is not really a guarantee of event execution, apart from some exeptions or when using jQuery (e.g. attaching the listeners in the proper sequence).

Now, how can I manage to execute click before opening? It occurs before, but somehow there should be another click listener attached to that anchor that is executed before.

Any idea?

mat_boy
  • 12,998
  • 22
  • 72
  • 116
  • If what you're really after is a way to force execution of code in opening handler to run after click handler, you can always use setTimeout("console.log('opening');",100); to delay – mjw Jun 08 '17 at 20:26
  • @mjw thanks for the feedback. Does not seem a reliable solution. What happens if the first click takes more than 100ms? – mat_boy Jun 08 '17 at 20:33
  • If it takes longer than 100ms to console.log idk what to tell you ;) – mjw Jun 08 '17 at 20:36
  • No, I mean, what if the function executed by the click events takes more than 100ms – mat_boy Jun 08 '17 at 20:38
  • 1
    @mjw anyway the solution works, I'm not 100% sure that it is reliable, but works. So, maybe you can post a full answer – mat_boy Jun 10 '17 at 10:11

1 Answers1

1

If what you're really after is a way to force execution of code in opening handler to run after click handler, you can always use setTimeout:

setTimeout("console.log('opening');",100);
mjw
  • 1,196
  • 1
  • 12
  • 19