'HTML onClick node js function loading 'require' another module in the onClick function not working [duplicate]
I have code like:
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onclick Event</h2>
<p>The onclick event triggers a function when an element is clicked on.</p>
<p>Click to trigger a function that will output "Hello World":</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
const os = require('os')
let hostName = os.hostname()
alert(hostName);
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
The code works when I remove:
const os = require('os')
hostName = os.hostname()
alert(hostName);
I however need to load a module in this function that is being used for the onClick event.
Please note that I am a noob to javascript. Any and all assistance / guidance will be much appreciated.
Thanks.
UPDATE:
for those coming to this page, check: Node-style require for in-browser javascript?
Solution 1:[1]
const button = document.getElementById('btn');
const demo = document.getElementById('demo');
button.onclick = function() {
demo.innerHTML = 'Hello World';
};
<button id="btn" >Click me</button>
<p id="demo"> </p>
If you want to load a module you need to show some of your node.js code as we don't see what that looks like and what you want to render.
I suggest reading the documentation of Node.js and EJS.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
