Vladislav Dimitrov
Vladislav Dimitrov

Reputation: 127

JavaScript listen for jQuery event

I'm having trouble passing an event from jQuery to JavaScript. I am setting the JS event listener first and then firing the jQuery event from an external file, but the listener never gets triggered. Here's some code:

Listener

<script type="text/javascript">
document.addEventListener('readyToPlay', loadLibs);

function loadLibs() {
    alert('success');
}

</script>
<script src="/assets/js/plugins/co/co.js" type="text/javascript"></script>

Included File - The alert returns (function), which means jQuery is loaded

function init() {
    alert(typeof jQuery);
    $(document).trigger('readyToPlay');
}

I am thinking the issue might be caused by referencing document in the external file, but I'm not sure how to get around that.

Upvotes: 3

Views: 1589

Answers (1)

gbalduzzi
gbalduzzi

Reputation: 10176

You can't use jQuery but you can fire it in Vanilla JS

function init() {
  var event= new CustomEvent('readyToPlay',[]);
  document.dispatchEvent(event);
}

Upvotes: 2

Related Questions