title: How do I trigger javascript from a button click
body: <p>I'm trying to trigger javascript to start, stop and restart a stopwatch from a button in the html file. What is the best way to pass an event from the html to the javascript?</p>
  
  <p>I have managed to trigger a console log when the button is pressed, but I can't put in the javascript in the second js file. I just receive errors. Would socket.io work? I have investigated Event emitters and listeners but I think it's out of my skill level.</p>
  
  <p>server.js</p>
  
  <pre><code>var express = require('express');
  var app = express();
  var path = require('path');
  var Stopwatch = require("node-stopwatch").Stopwatch;
  const EventEmitter = require('events');
  
  var stopwatch = Stopwatch.create();
  
  app.use(express.static('public'));
  
  // start the express web server listening on 8080
  app.listen(8080, () =&gt; {
    console.log('listening on 8080');
  });
  
  // serve the homepage
  app.get('/', (req, res) =&gt; {
    res.sendFile(__dirname + '/index.html');
  });
  
  stopwatch.start();
  
  /*
  stopwatch output test
  */
  
  console.log("ticks: " + stopwatch.elapsedTicks);
  console.log("milliseconds: " + stopwatch.elapsedMilliseconds);
  console.log("seconds: " + stopwatch.elapsed.seconds);
  console.log("minutes: " + stopwatch.elapsed.minutes);
  console.log("hours: " + stopwatch.elapsed.hours);
  
  //stop it now
  stopwatch.stop();
  
  class MyEmitter extends EventEmitter {}
  
  const myEmitter = new MyEmitter();
  myEmitter.on('event', () =&gt; {
    console.log('an event occurred!');
  });
  </code></pre>
  
  <p>client.js</p>
  
  <pre><code>console.log('Client-side code running');
  
  
  const button = document.getElementById('myButton');
  button.addEventListener('click', function(e) {
    console.log('button was clicked');
  });
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Stopwatch&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Stopwatch&lt;/h1&gt;
      &lt;p id="counter"&gt;Loading button click data.&lt;/p&gt;
      &lt;button id="myButton"&gt;Click me!&lt;/button&gt;
    &lt;/body&gt;
    &lt;script src="client.js"&gt;&lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I expected to trigger the javascript on button click but I cannot run it in client.js</p>