3

I have created a app.js (and app.scss) to use with Symfony Encore.

└───assets
    ├───css
    │   └───app.scss
    └───js
        └───app.js

app.js:

/**
 * Name: app.js
 * Desc: Main application javascript
 */

var $ = require('jquery');
require('bootstrap-sass');
require('../css/app.scss');

// Initialize tooltips
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
});

var openLoginModal = function(element){
    $('#login-modal').modal('show');
}

In one of my templates, I want to be able to set the 'onclick' event to call the 'openLoginModal' function defined in app.js

<a class="btn btn-info navbar-btn navbar-right" data-toggle="login-modal" onclick="openLoginModal(this)">Login</a>

When the anchor element is clicked the following error is displayed in the console:

Uncaught ReferenceError: openLoginModal is not defined
    at HTMLAnchorElement.onclick ((index):29)

I assume that I cannot access the functions from app.js for some reason, but I don't know enough about webpack to understand why this is

2
  • 1
    Try to change var openLoginModal to Window.prototype.openLoginModal. I believe you are trying to access a global function, but your script is inside a webpack module. Commented Mar 18, 2018 at 8:34
  • I just answered this here stackoverflow.com/a/49316775/9083959 Commented Mar 18, 2018 at 13:19

1 Answer 1

1

try this

(function ($, window)
{
   window.openLoginModal = function(element){
       $('#login-modal').modal('show');
   }
}(jQuery, window);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.