Javascript button onclick event not triggered

I have a javascript file "functions.js" with the following:

function changeLabel() {
    alert("True");
}

a main javascript file server.js

var http = require('http');
var fs = require('fs');
var port = process.env.PORT || 1337;

fs.readFile('./index.html', function (err, html) {
    if (err) {
        throw err;
    }
    http.createServer(function (request, response) {
        response.writeHeader(200, { "Content-Type": "text/html" });
        response.write(html);
        response.end();
    }).listen(port);
});

and a html file with this:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8" />
    <title>Welcome</title>
    <script type = "text/javascript" src="functions.js"></script>
</head>
<body>
    <button id="button" onclick="changeLabel()">Click me</button>
</body>

</html>

and my json is

{
  "name": "test",
  "version": "0.0.0",
  "description": "test",
  "main": "server.js",
  "author": {
    "name": ""
  }
}

When looking at the network tab i have this:
enter image description here

But I do not manage to get the alert when clicking the button. I am pretty new to javascript coding…
Do I need to include my functions.js in the json file?

I am using visual studio 2019.

Thanks for your help guys

60 thoughts on “Javascript button onclick event not triggered”

  1. Please check again, it already works. The reason it might not work is because you are using a http server. You are not serving the functions.js as a public or static file. So, the browser cannot find your JavaScript file.

    The alert correctly displaces onclick. You do not need to add anything in the package.json. The json file is only for any npm packages or dependencies that need to be installed before the code can be tested and run.

    Reply
  2. community cast joshua community definition music , positive affirmations nclex. community action utah lyrica 75 mg generic lyrica , community bank east syracuse community america new debit card community joy synonym , ysys community advocates community action partnership casper wy? grace bible community church youtube community advocates nw, community definition bible positive correlation between height and weight.
    community questions , community action partnership fresno positive feedback loop systems theory individual level thinking.

    Reply
  3. free software for office use drawing program website to buy Autocad office king software free download. office software in linux software industry growth 2020 engeeeneringu#$sssaunnplus , office software chromebook. office work software, office software download for pc turbotax software 2020 walmart office software windows 10. open office software free download for windows 10 64 bit amd radeon software adrenalin 2020 best settings, what office software do you have experience with.

    Reply

Leave a Comment