mercredi 11 octobre 2017

Laravel - onclick function is not defined only after compiling the JS file

So I'm working with Laravel 5.4 and in the project I have a blade view with this element:

<a onclick="getStuff(this)"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>

But when I click on the element I get this error:

ReferenceError: getStuff is not defined

This is in the external JS I'm using:

$(window).on("load", function () { code... }
...
function getStuff(aElement) { code... }

The thing is, all of it actually works perfectly when I add the external JS file directly into the public/js folder in the main directory (which is the only folder that the blade view can have access to). But, as I take it, this is bad practice. What I'm doing ("the Laravel way") is to add the JS file in the resource/assets/js folder and then running npm run dev to compile it so it automatically adds a compiled version of the file into the public/js folder. It is after doing that when it doesn't work.

I also think the compilation is moderately successful because I can run everything in the JS file that doesn't require an interaction with the page to run. It's only when I try to call it with something like that onclick that it doesn't work.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire