0


Hello,
I downloaded electron and installed it on webstorm in order to learn it. I also installed Jquery using the terminal and including in my scripts but jquery isn't working but it is recognized in my code (I haven't got any error).

My code :

const remote = require('electron').remote;
let window = remote.getCurrentWindow();

function exitGame() {
    alert("test");
    $('exit').click(() => {
        alert("closing");
        window.close();
    })
}

exitGame();
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Electron Test</title>
    <!-- RESET CSS -->
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <!-- ADDING CSS -->
    <link rel="stylesheet" type="text/css" href="css/main_menu.css">
  </head>
  <body>

    <div id="exit">Leave the game</div>

    <!-- ADDING JAVASCRIPT -->
    <script>
      // You can also require other files to run in this process
      require('./renderer.js');
      require('./js/jquery.min.js');
      require('./js/main_menu.js');
    </script>
  </body>
</html>

The jQuery alert is working but the click isn't.
Even if I replace the "exit" id by "body" it's not working...

Can you tell me what's wrong ?
Thank you very much, best regards.
  • Duplicate of: https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined – Gernot Feb 13 '18 at 10:12
  • Possible duplicate of [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) – Gernot Feb 13 '18 at 10:12
  • `$(document).on('click', '#exit', () => { alert("closing"); window.close(); })` – connexo Feb 13 '18 at 18:01
  • Possible duplicate of [Electron: jQuery is not defined](https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined) – Tim Feb 14 '18 at 12:33
  • dupe of https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined – Tim Feb 14 '18 at 12:33

2 Answers2

1

exit is an id and not some element. So you need to use # before it.

$('#exit').click(() => {
    alert("closing");
    window.close();
})

Better way to check jQuer is wokring or not could be:

if(!jQuery)
{
   alert("Not working!");
}
void
  • 36,090
  • 8
  • 62
  • 107
  • I tried using #exit but it's still not working. The function you said me if(!jQuery) { alert("Not working!");} This doesn't pop up any alert so jQuery is working. But the click does not, I can't get the reason why it doesn't –  Feb 13 '18 at 10:15
  • Try doing `jQuery('#exit').click((` – void Feb 13 '18 at 14:54
  • This is also not working. It was working on vanilla JS but not on jQuery. Is Electron not compatible with jQuery ? –  Feb 13 '18 at 17:49
0

Alright, made it working by editing the tag with the code requiere jQuery.
Before Electron's base script tag, I added a tag with my jQuery. It seems that it wasn't loaded