Can't get jquery plugin to work


#1

Electron version: 1.2.0
Operating system: OS X

Hello, I’m trying to use the jquery plugin chosen in my electron app. When I look at sources in the developer panel I see the chosen js has loaded, but then when I try to invoke the code I get the following error:

Uncaught TypeError: $(…).chosen is not a function
Here’s how I’m loading it up from index.html:

<script>
 window.$ = window.jQuery = require('jquery');
 require('./renderer.js')  
</script>
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<script>if (window.module) module = window.module; var $ = require('jQuery');</script>
<script src="./chosen/chosen.jquery.min.js"></script>

I also tried using this line instead, but then the file doesn’t even get loaded up:
require('./chosen/chosen.jquery.min.js');

Jquery works perfectly, BTW.


#2

What is ./chosen/chosen.jquery.min.js?

window.$ = window.jQuery = require(./wherever-jquery-is/jquery.min.js);” should work.


#3

Chosen is a jquery plugin:
https://harvesthq.github.io/chosen/


#4

Oh, I see, so when you say jQuery’s working, it’s that jQuery works, but this plugin doesn’t.

Is it a scoping issue? Because you’ve got var $ = ..., when you’re already setting it on window up top? So the plugin is using the one on window, but you’re calling the ‘scoped’ var?


#5

I managed to get jQuery working by including it as a script in the html. Ensure that it’s included first before your other jquery plugins.

  <head>
    <meta charset="UTF-8">
    <title>My App</title>
    <script type="text/javascript" src="./node_modules/jquery/dist/jquery.min.js" onload="window.$ = window.jQuery = module.exports;"></script>
    <script src="./node_modules/bootstrap-sass/assets/javascripts/bootstrap.js"></script>
    <script src="./node_modules/hammerjs/hammer.min.js"></script>
    <script src="./node_modules/velocity-animate/velocity.min.js"></script>
    <link href="css/index.css" rel="stylesheet" type="text/css" />
</head>